A SQL query but in Excel and be done in VBA code

ShuStar

Board Regular
Joined
Sep 9, 2015
Messages
89
Office Version
  1. 2010
Platform
  1. Windows
Hi Gurus,

I am trying to Join 2 tables together from 2 different sheets on Excel (but they are in the same workbook) - any idea how I can do this via VBA code?

I can do this on a basic SQL query which would look something like:
Select Columns A:F in Raw Data 2 sheet & Columns A:D in Matrix sheet
From Raw Data 2 sheet
Inner Join Matrix sheet
On Raw Data 2 ID (which is Column A) = Matrix ID (Column A)

For both sheets - A2 is column headers, and row 3 downwards are the data values for each column

How could I do this on Excel VBA where the data would be joined into a new tab called Consolidated Data? Any idea?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi all,

update:

Ultimately, via vba code, aiming to merge data from two tabs together, where:
Consolidated Tab - this is the tab where I would like to merge Matrix and Raw data 2 tab together
Raw data 2 tab - the ID is only here once (unique/distinct)
Matrix tab - the same ID can appear more than once

I don't think an index match would work here for me (unless anyone knows how maybe); it's probably not even the right sql query i mentioned above (Union is probably more appropriate rather than a inner join..) I am struggling to put this issue into words lol

An example sheet can be found in this link - it may help visualise this issue I am facing - Mapping Filter Doc

Many thanks to anyone who can support.
 
Upvote 0
Ranges I am working with:

Ultimately, via vba code, aiming to merge data from two tabs together, where:
Consolidated Tab - this is the tab where I would like to merge Matrix and Raw data 2 tab together - plot data from A3 onwards/downwards
Raw data 2 tab - the ID is only here once (unique/distinct) - Range = A3:Fx (x being the last row of table set)
Matrix tab - the same ID can appear more than once - Range = A3:Dx (x being the last row of table set)
 
Upvote 0
Hi all,

**Admins can delete posts #1,2,3 to declutter this thread as I have tried to re-summarise the ask as below***

I am trying to attach an image to help explain better what i am trying to achieve but it is too large. An example sheet can be found here - Mapping Filter Doc

Effectively, I'm trying to merge 2 worksheets in the same workbook on what would otherwise be a union function in SQL or MS Access (unless I am losing the plot)

I have looked at vlookup, index match, some Union vba code, Data Consolidation, Pivot Table (via Alt+D+P method) but none seem to be able to resolve.

Could do with trying to work out the VBA code to apply the below logic:
For each ID in Raw Data 2 Tab, match it to the IDs in Matrix Tab and plot the Range Columns & data into a new Tab called Consolidated (the columns to pull over have been noted below).

Ranges I am working with:
Raw data 2 tab - the ID for each row is here appears only once (ie ID is unique/distinct). Table Range = A3:Fx (x being the last row of table set)
Matrix tab - the same ID can appear more than once. Table Range = A3:Dx (x being the last row of table set). Note: if it helps knowing this in any way, in Matrix Tab: ID and Names columns combined is what would make the dataset unique/distinct here
Consolidated Tab - this is the tab where I would like to merge Matrix and Raw data 2 tab together - plot data from A3 onwards/downwards
*Bonus Point - If there is no match between Raw Data ID and Matrix ID - in the new Consolidated tab place comments in the Names column as "No Match"

Would be ever so grateful to anyone who can help me crack this.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,530
Members
448,969
Latest member
mirek8991

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top