VBA To Merge 2 Excel Sheets

madkinson

Board Regular
Joined
Dec 17, 2004
Messages
113
Office Version
  1. 365
Platform
  1. Windows
I have a template file that is used to load data into a 3rd party application.
White_Eagle_Rail_Car_Data (4).xlsx
EFGHIJKLMNO
1LocationDateTrain #Ref #PositionVehicle IDGrossNetQtyLoad StatusIn/Out Status
2109100EI
3109100EI
4109100EI
5109100EI
6109100EI
Dataset 1
Cell Formulas
RangeFormula
K2:K6K2=M2*2000


I also have another file created in a 3rd application.
White_Eagle_Rail_Car_Data (4).xlsx
ABC
1DateRef #Vehicle ID
211/05/231PMRX10303
311/05/232PMRX10291
411/05/233PMRX10263
511/05/234PMRX10255
611/05/235PMRX10297
Dataset 1


I want to merge them so I will end up with something like this.
White_Eagle_Rail_Car_Data (4).xlsx
EFGHIJKLMNO
1LocationDateTrain #Ref #PositionVehicle IDGrossNetQtyLoad StatusIn/Out Status
210911/5/2023111PMRX103032360000118.00EI
310911/5/2023122PMRX102912360000118.00EI
410911/5/2023133PMRX102632360000118.00EI
510911/5/2023144PMRX102552360000118.00EI
610911/5/2023155PMRX102972360000118.00EI
7
8Calculated
9Merged
Dataset 1
Cell Formulas
RangeFormula
I2:I6I2=H2
K2:K6K2=M2*2000


For example, cell A2 in example 2 should end up in cell F2 in example 1. Any assistance is greatly appreciated. thanks!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
I don't see a common value/ID that will allow the merger of the two tables. Please clarify. Also don't see the source value for the quantity.
 
Upvote 0
My bad. I cut off the Qty in the second sheet.

What if I used "Position" in the template to match up with "Ref" in the second sheet? They are always unique.
 
Upvote 0
Show us an updated sample and a mocked up solution so that we understand clearly your needs.
 
Upvote 0
File A is the template
Car Data Template.xlsx
ABCDEFGHIJK
1LocationDateTrain #Ref #PositionVehicle IDGrossNet Qty Load StatusIn/Out Status
21091100EI
31091200EI
41091300EI
51091400EI
61091500EI
71091600EI
81091700EI
91091800EI
101091900EI
1110911000EI
Sheet1
Cell Formulas
RangeFormula
G2:G11G2=I2*2000


File B is the data from a PLC
Car Data from PLC.xlsx
ABCD
1DateRef #Vehicle IDWeight
211/05/231PMRX10303118.00
311/05/232PMRX10291118.00
411/05/233PMRX10263118.00
511/05/234PMRX10255118.00
611/05/235PMRX10297118.00
711/05/236PMRX10212118.00
811/05/237PMRX10289118.00
911/05/238PMRX10293118.00
1011/05/239PMRX10201118.00
1111/05/2310PMRX10275118.00
Dataset 1


File C is what I would like to end up with.
Car Data Template.xlsx
ABCDEFGHIJK
1LocationDateTrain #Ref #PositionVehicle IDGrossNet Qty Load StatusIn/Out Status
210911/5/2023111PMRX103032360000118.00EI
310911/5/2023122PMRX102912360000118.00EI
410911/5/2023133PMRX102632360000118.00EI
510911/5/2023144PMRX102552360000118.00EI
610911/5/2023155PMRX102972360000118.00EI
710911/5/2023166PMRX102122360000118.00EI
810911/5/2023177PMRX102892360000118.00EI
910911/5/2023188PMRX102932360000118.00EI
1010911/5/2023199PMRX102012360000118.00EI
1110911/5/202311010PMRX102752360000118.00EI
12
13
14Merged from File
15Calculated
Sheet1
Cell Formulas
RangeFormula
G2:G11G2=I2*2000



The "Ref#" from the PLC file should match the "Position" in the template on a 1:1 basis.
 
Upvote 0
OK. Thanks for the update. Here is a Power Query Solution. I see that Qty is actually the Weight. Need to change the Mcode to make that happen. See next post for updated Mcode.

Power Query:
let
    T1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    T2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    MQ = Table.NestedJoin(T1, {"Position"},T2, {"Ref #"}, "Table2", JoinKind.FullOuter),
    ET = Table.ExpandTableColumn(MQ, "Table2", {"Date", "Ref #", "Vehicle ID", "Weight"}, {"Date.1", "Ref #.1", "Vehicle ID.1", "Weight"}),
    RC = Table.RemoveColumns(ET,{"Date", "Ref #", "Vehicle ID", "Weight"}),
    CT = Table.TransformColumnTypes(RC,{{"Date.1", type date}}),
    MoveColumns = Table.ReorderColumns(CT,{"Location", "Date.1", "Train #", "Ref #.1", "Position", "Vehicle ID.1", "Gross", "Net", " Qty ", "Load Status", "In/Out Status"})

in
    MoveColumns

Book14
ABCDEFGHIJKLMNOP
1LocationDateTrain #Ref #PositionVehicle IDGrossNet Qty Load StatusIn/Out StatusDateRef #Vehicle IDWeight
21091100EI11/5/20231PMRX10303118
31091200EI11/5/20232PMRX10291118
41091300EI11/5/20233PMRX10263118
51091400EI11/5/20234PMRX10255118
61091500EI11/5/20235PMRX10297118
71091600EI11/5/20236PMRX10212118
81091700EI11/5/20237PMRX10289118
91091800EI11/5/20238PMRX10293118
101091900EI11/5/20239PMRX10201118
1110911000EI11/5/202310PMRX10275118
12
13LocationDate.1Train #Ref #.1PositionVehicle ID.1GrossNet Qty Load StatusIn/Out Status
1410911/5/2023111PMRX1030300EI
1510911/5/2023122PMRX1029100EI
1610911/5/2023133PMRX1026300EI
1710911/5/2023144PMRX1025500EI
1810911/5/2023155PMRX1029700EI
1910911/5/2023166PMRX1021200EI
2010911/5/2023177PMRX1028900EI
2110911/5/2023188PMRX1029300EI
2210911/5/2023199PMRX1020100EI
2310911/5/202311010PMRX1027500EI
Sheet1
Cell Formulas
RangeFormula
G2:G11G2=I2*2000
 
Upvote 0
Solution
updated Mcode to reflect change of names

Power Query:
let
    T1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    T2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    MQ = Table.NestedJoin(T1, {"Position"},T2, {"Ref #"}, "Table2", JoinKind.FullOuter),
    ET = Table.ExpandTableColumn(MQ, "Table2", {"Date", "Ref #", "Vehicle ID", "Weight"}, {"Date.1", "Ref #.1", "Vehicle ID.1", "Weight"}),
    RC = Table.RemoveColumns(ET,{"Date", "Ref #", "Vehicle ID", " Qty "}),
    Rename = Table.RenameColumns(RC,{{"Weight", "QTY"}, {"Date.1", "Date"}, {"Ref #.1", "Ref #"}, {"Vehicle ID.1", "Vehicle ID"}}),
    CT = Table.TransformColumnTypes(Rename,{{"Date", type date}}),
    MoveColumns = Table.ReorderColumns(CT,{"Location", "Date", "Train #", "Ref #", "Position", "Vehicle ID", "Gross", "Net", "QTY", "Load Status", "In/Out Status"})

in
    MoveColumns
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

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