VBA to merge data from different workbooks (Excel 2010)

smaier69

New Member
Joined
Nov 10, 2017
Messages
1
Hello, all.

What I am trying to do is run a macro to merge data from different workbooks and deposit it into a "master" workbook that already has previous data. The way I originally wrote it had the following steps:

  1. Open the (lets call it the aggregator) workbook that will handle the merging. It has 2 tabs, one with the button to run the VBA and one to act as the middle man for the data.
  2. Click the ActiveX button which opens the first workbook with data, copies all the rows of data into the aggregator's second tab, deletes the source data, then saves and closes the first workbook with data (now empty).
  3. Repeat the previous step for the next 5 data source workbooks.
  4. Open the "master" workbook, find the first empty row and cut/paste the data from the aggregator's 2nd tab into the master.
  5. Save and close the master.

Below is the code I fabricobbled up. The issue is sometimes it works flawlessly, but sometimes it doesn't copy all the rows of data from the source workbooks. It'll leave the last 1 to 8 or so rows and everything before that will have been cut/pasted out into the aggregator then into the master. The number of leftover rows and which data workbooks this happens in seems to be totally random.


Code:
Private Sub CommandButton1_Click()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.Workbooks.Open ("[URL="file://\\server01\PSI-Share\Inspection"]\\[/URL]Applied Labor Data\ShopPC4.xlsx")
    Worksheets("Work Data").Activate
    Worksheets("Work Data").Select
    
    
lastCol = 28

LastRow = ActiveSheet.Cells(65536, lastCol).End(xlUp).Row
ActiveSheet.Range("a1", ActiveSheet.Cells(LastRow, lastCol)).Select
Selection.Cut
Workbooks("Auto Merger.xlsm").Worksheets("Data Sheet").Activate
Workbooks("Auto Merger.xlsm").Worksheets("Data Sheet").Select
ActiveSheet.Range("a1").Select
ActiveSheet.Paste
Workbooks("ShopPC4.xlsx").Close SaveChanges:=True

Application.Workbooks.Open ("[URL="file://\\server01\PSI-Share\Inspection"]\\[/URL]Applied Labor Data\ShopPC2.xlsx")
    Worksheets("Work Data").Activate
    Worksheets("Work Data").Select
    
    
lastRow2 = ActiveSheet.Cells(65536, lastCol).End(xlUp).Row
ActiveSheet.Range("a1", ActiveSheet.Cells(lastRow2, lastCol)).Select
Selection.Cut
Workbooks("Auto Merger.xlsm").Worksheets("Data Sheet").Activate
Workbooks("Auto Merger.xlsm").Worksheets("Data Sheet").Select
ActiveSheet.Range("a1").End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
Workbooks("ShopPC2.xlsx").Close SaveChanges:=True
 
Application.Workbooks.Open ("[URL="file://\\server01\PSI-Share\Inspection"]\\[/URL]Applied Labor Data\ShopPC3.xlsx")
    Worksheets("Work Data").Activate
    Worksheets("Work Data").Select
    
    
lastRow3 = ActiveSheet.Cells(65536, lastCol).End(xlUp).Row
ActiveSheet.Range("a1", ActiveSheet.Cells(lastRow3, lastCol)).Select
Selection.Cut
Workbooks("Auto Merger.xlsm").Worksheets("Data Sheet").Activate
Workbooks("Auto Merger.xlsm").Worksheets("Data Sheet").Select
ActiveSheet.Range("a1").End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
Workbooks("ShopPC3.xlsx").Close SaveChanges:=True
 
Application.Workbooks.Open ("[URL="file://\\server01\PSI-Share\Inspection"]\\[/URL]Applied Labor Data\ShopPC1.xlsx")
    Worksheets("Work Data").Activate
    Worksheets("Work Data").Select
    
    
lastRow4 = ActiveSheet.Cells(65536, lastCol).End(xlUp).Row
ActiveSheet.Range("a1", ActiveSheet.Cells(lastRow4, lastCol)).Select
Selection.Cut
Workbooks("Auto Merger.xlsm").Worksheets("Data Sheet").Activate
Workbooks("Auto Merger.xlsm").Worksheets("Data Sheet").Select
ActiveSheet.Range("a1").End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
Workbooks("ShopPC1.xlsx").Close SaveChanges:=True

Application.Workbooks.Open ("[URL="file://\\server01\PSI-Share\Inspection"]\\[/URL]Applied Labor Data\ShopPC5.xlsx")
    Worksheets("Work Data").Activate
    Worksheets("Work Data").Select
 
    
lastRow5 = ActiveSheet.Cells(65536, lastCol).End(xlUp).Row
ActiveSheet.Range("a1", ActiveSheet.Cells(lastRow5, lastCol)).Select
Selection.Cut
Workbooks("Auto Merger.xlsm").Worksheets("Data Sheet").Activate
Workbooks("Auto Merger.xlsm").Worksheets("Data Sheet").Select
ActiveSheet.Range("a1").End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
Workbooks("ShopPC5.xlsx").Close SaveChanges:=True
Application.Workbooks.Open ("[URL="file://\\server01\PSI-Share\Inspection"]\\[/URL]Applied Labor Data\ShopPC6.xlsx")
    Worksheets("Work Data").Activate
    Worksheets("Work Data").Select
 
    
lastRow6 = ActiveSheet.Cells(65536, lastCol).End(xlUp).Row
ActiveSheet.Range("a1", ActiveSheet.Cells(lastRow6, lastCol)).Select
Selection.Cut
Workbooks("Auto Merger.xlsm").Worksheets("Data Sheet").Activate
Workbooks("Auto Merger.xlsm").Worksheets("Data Sheet").Select
ActiveSheet.Range("a1").End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
Workbooks("ShopPC6.xlsx").Close SaveChanges:=True
 
 
 
 
Workbooks("Auto Merger.xlsm").Sheets("Data Sheet").Select
Workbooks("Auto Merger.xlsm").Sheets("Data Sheet").Activate
LastRow7 = ActiveSheet.Cells(65536, lastCol).End(xlUp).Row
ActiveSheet.Range("a1", ActiveSheet.Cells(LastRow7, lastCol)).Select
Selection.Cut

Workbooks.Open ("[URL="file://\\server01\PSI-Share\Inspection"]\\[/URL]Applied Labor Data\Work Form Data.xlsx"), Password:="Password001"
Workbooks("Work Form Data.xlsx").Sheets("Work Data").Select
Workbooks("Work Form Data.xlsx").Sheets("Work Data").Activate

ActiveSheet.Range("a1").End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Workbooks("Work Form Data.xlsx").Close SaveChanges:=True
Workbooks("Auto Merger.xlsm").Sheets("Button Sheet").Select
MsgBox ("All Done!")
End Sub

I am sure this could be done in a much much easier way with many fewer lines. What's driving me nuts is why it's not working. It seems so straightforward.

Am I overlooking something or should I just dump this method and try something else?

Any help would be greatly appreciated.

Cheers!
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
The row your macro stops at when cutting the data out is not random, it is actually set to a specific number:

lastRowX = ActiveSheet.Cells('65536', lastCol).End(xlUp).Row

Try this, edits have been made bold:

Code:
Private Sub CommandButton1_Click()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.Workbooks.Open ("[URL="file://\\Applied"]\\Applied[/URL] Labor Data\ShopPC4.xlsx")
    Worksheets("Work Data").Activate
    Worksheets("Work Data").Select
    
    
lastCol = 28

[B]LastRow = ActiveSheet.UsedRange.Rows.Count[/B]
ActiveSheet.Range("a1", ActiveSheet.Cells(LastRow, lastCol)).Select
Selection.Cut
Workbooks("Auto Merger.xlsm").Worksheets("Data Sheet").Activate
Workbooks("Auto Merger.xlsm").Worksheets("Data Sheet").Select
ActiveSheet.Range("a1").Select
ActiveSheet.Paste
Workbooks("ShopPC4.xlsx").Close SaveChanges:=True
Application.Workbooks.Open ("[URL="file://\\Applied"]\\Applied[/URL] Labor Data\ShopPC2.xlsx")
    Worksheets("Work Data").Activate
    Worksheets("Work Data").Select
    
    
[B]lastRow2 = ActiveSheet.UsedRange.Rows.Count[/B]
ActiveSheet.Range("a1", ActiveSheet.Cells(lastRow2, lastCol)).Select
Selection.Cut
Workbooks("Auto Merger.xlsm").Worksheets("Data Sheet").Activate
Workbooks("Auto Merger.xlsm").Worksheets("Data Sheet").Select
ActiveSheet.Range("a1").End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
Workbooks("ShopPC2.xlsx").Close SaveChanges:=True
 
Application.Workbooks.Open ("[URL="file://\\Applied"]\\Applied[/URL] Labor Data\ShopPC3.xlsx")
    Worksheets("Work Data").Activate
    Worksheets("Work Data").Select
    
    
[B]lastRow3 = ActiveSheet.UsedRange.Rows.Count[/B]
ActiveSheet.Range("a1", ActiveSheet.Cells(lastRow3, lastCol)).Select
Selection.Cut
Workbooks("Auto Merger.xlsm").Worksheets("Data Sheet").Activate
Workbooks("Auto Merger.xlsm").Worksheets("Data Sheet").Select
ActiveSheet.Range("a1").End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
Workbooks("ShopPC3.xlsx").Close SaveChanges:=True
 
Application.Workbooks.Open ("[URL="file://\\Applied"]\\Applied[/URL] Labor Data\ShopPC1.xlsx")
    Worksheets("Work Data").Activate
    Worksheets("Work Data").Select
    
    
[B]lastRow4 = ActiveSheet.UsedRange.Rows.Count[/B]
ActiveSheet.Range("a1", ActiveSheet.Cells(lastRow4, lastCol)).Select
Selection.Cut
Workbooks("Auto Merger.xlsm").Worksheets("Data Sheet").Activate
Workbooks("Auto Merger.xlsm").Worksheets("Data Sheet").Select
ActiveSheet.Range("a1").End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
Workbooks("ShopPC1.xlsx").Close SaveChanges:=True
Application.Workbooks.Open ("[URL="file://\\Applied"]\\Applied[/URL] Labor Data\ShopPC5.xlsx")
    Worksheets("Work Data").Activate
    Worksheets("Work Data").Select
 
    
[B]lastRow5 = ActiveSheet.UsedRange.Rows.Count[/B]
ActiveSheet.Range("a1", ActiveSheet.Cells(lastRow5, lastCol)).Select
Selection.Cut
Workbooks("Auto Merger.xlsm").Worksheets("Data Sheet").Activate
Workbooks("Auto Merger.xlsm").Worksheets("Data Sheet").Select
ActiveSheet.Range("a1").End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
Workbooks("ShopPC5.xlsx").Close SaveChanges:=True
Application.Workbooks.Open ("[URL="file://\\Applied"]\\Applied[/URL] Labor Data\ShopPC6.xlsx")
    Worksheets("Work Data").Activate
    Worksheets("Work Data").Select
 
    
[B]lastRow6 = ActiveSheet.UsedRange.Rows.Count[/B]
ActiveSheet.Range("a1", ActiveSheet.Cells(lastRow6, lastCol)).Select
Selection.Cut
Workbooks("Auto Merger.xlsm").Worksheets("Data Sheet").Activate
Workbooks("Auto Merger.xlsm").Worksheets("Data Sheet").Select
ActiveSheet.Range("a1").End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
Workbooks("ShopPC6.xlsx").Close SaveChanges:=True
 
 
 
 
Workbooks("Auto Merger.xlsm").Sheets("Data Sheet").Select
Workbooks("Auto Merger.xlsm").Sheets("Data Sheet").Activate
LastRow7 = ActiveSheet.Cells(65536, lastCol).End(xlUp).Row
ActiveSheet.Range("a1", ActiveSheet.Cells(LastRow7, lastCol)).Select
Selection.Cut
Workbooks.Open ("[URL="file://\\Applied"]\\Applied[/URL] Labor Data\Work Form Data.xlsx"), Password:="Password001"
Workbooks("Work Form Data.xlsx").Sheets("Work Data").Select
Workbooks("Work Form Data.xlsx").Sheets("Work Data").Activate
ActiveSheet.Range("a1").End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Workbooks("Work Form Data.xlsx").Close SaveChanges:=True
Workbooks("Auto Merger.xlsm").Sheets("Button Sheet").Select
MsgBox ("All Done!")
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,830
Messages
6,181,225
Members
453,025
Latest member
Hannah_Pham93

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