2

I am not sure what approach to take but wondering can I join data from two excel sheets and populate in a 3rd sheet like how we join in SQL using VBA. Example data is below, I would like to join Sheet1 and Sheet2 by Emp_id and populate the result in Sheet3. I have googled a lot and tried whatever I know but nowhere I am close to get this, can someone throw some light on this or share an working example, that would be great. I am still trying and will post if I got a solution.

Sample Data

3
  • 1
    You could copy and paste sheet 2 into sheet three then use a vlookup. Commented Jun 4, 2016 at 1:45
  • Do you have Excel 2016? Then you'll find within the Data tab the Get & Transform area with the option to New QueryCombine QueriesMerge. This will (almost) automatically merge both tables for you with an automated background refresh if any of the underlying table changes. Otherwise, you can write an ADO query. For more on this read this or search for similar solutions on SO. Commented Jun 4, 2016 at 12:50
  • Thanks @Ralph, I will try the ADO method, I am using 2013 version and my sample data was too simple compare to actual, that's why I felt this may not be easily done using vlookup. I will try ADO and share the outcome. Commented Jun 5, 2016 at 3:47

2 Answers 2

1

Below works for mine, again below query is for the sample data. For my actual data, I had to get the sheet number to use sheet name (example [Sheet2$] is replaced with [Sheet" & Sheets("Source").Index & "$] to use "Source" as the sheet in place of "Sheet2".

I have to use LEFT JOIN because I am getting error with only JOIN and later deleted the extra rows from Sheet3 for the records not matching in Sheet1, also you may have to use UNION function since FULL JOIN is not supported in case if you want data from both the table.

Hope this helps for someone...

Option Explicit

Sub JoinTables()

 Dim cn As ADODB.Connection
 Set cn = New ADODB.Connection


 With cn
     .Provider = "Microsoft.Jet.OLEDB.4.0"
     .ConnectionString = "Data Source=" & ThisWorkbook.Name & ";" & _
         "Extended Properties=Excel 8.0;"
     .Open
 End With

 Dim rs As ADODB.Recordset
 Set rs = New ADODB.Recordset

 rs.Open "SELECT * FROM [Sheet2$] LEFT JOIN [Sheet1$] ON [Sheet2$].[Emp_id] = [Sheet1$].[Emp_id]", cn

 With Worksheets("Sheet3")
     .Cells(2, 1).CopyFromRecordset rs
 End With

 rs.Close
 cn.Close

 End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Instead of Index to get sheet number [SHEET NAME$] also can be used.
0
cn As New ADODB.Connection 
rec As New ADODB.Recordset
cn.ConnectionString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & _
ActiveWorkbook.Path & Application.PathSeparator & ActiveWorkbook.Name

SQLquery = "SELECT [Sheet2$].[Emp_id],[Sheet2$].[Hobies],[Sheet1$].[Salary] FROM [Sheet2$] INNER JOIN [Sheet1$]  ON [Sheet2$].[Emp_id]=[Sheet1$].[Emp_id]"

rec.Open SQLquery, cn, adOpenKeyset, adLockOptimistic
Sheets("Sheet3").Range("A1").CopyFromRecordset rec

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.