We help IT Professionals succeed at work.

Excel VBA - Join/merge two ranges as you would with SQL

Murray Brown
Murray Brown asked
on
8,273 Views
Last Modified: 2013-12-05
Hi

I have two ranges that come from separate database tables pasted into Excel
They both have a column called ID that I want to use to merge the data as one would
do in a SQL join query. What VBA code would I use to do this?
Comment
Watch Question

Saqib HusainEngineer
CERTIFIED EXPERT

Commented:
What happens if the data is different for the two IDs?
Do you think the ODBC is suitable for your case?
Rory ArchibaldGrand Poobah
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2011

Commented:
There are various different ways. Which is best would depend on what you are doing with the data, but it would be simpler to extract the data using a query that joins the two tables in the first place if you can?
If the two tables contain the same sort of data,
can't you use a Union query to combine them?

Select table1.id, table1.field from table1
Union
Select table2.id,table2.otherfield from table2

The two queries combined by Union must have the same typeset of Felds in their Field definition, but they do not need to have the same names normally.

Hope this helps
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
Hi

This has to be done in Excel. The tables come from a SAP dump where there is a lot of security around user permissions etc
Saqib HusainEngineer
CERTIFIED EXPERT

Commented:
If you can give more details (preferably with a sample excel file) then probably someone can write a macro for you.
Yes, agreed!
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
Thanks for the help
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.