Merging Multiple Workbooks into Master Wbook

dellzy

Board Regular
Joined
Apr 24, 2013
Messages
146
Hi Excel Experts,

I found this awesome vba code to allow me copy every wsheet from multiple workbooks into one Master Wbook, then move the file to folder named "Done" in the same Master Wbook path. However, when it finds that the file it moves already exist in folder "Done", the macro will stop and point to the Master Workbook.

I need it to be able to rename the file (increment by 1 each time find existing) in parent path (maintaining the same file extension as original) then move to the "Done" folder.

My edited code successfully move the file if found the file not exist in "Done" folder, but still not working if the file has existed. Please help to correct my code:-
Code:
Do While FileName <> ""
  
Workbooks.Open FileName:=myPath & FileName, local:=True, ReadOnly:=True

For Each Sheet In ActiveWorkbook.Sheets
     Sheet.Copy after:=ThisWorkbook.Sheets("Macros")
Next Sheet

Workbooks(FileName).Close savechanges:=False
      If FSO.FileExists(ToDir & FileName) = False Then
      MsgBox "file doesn't exist"
      FSO.MoveFile Source:=myPath & FileName, Destination:=ToDir
    Else
      MsgBox "File exist"
      NewName = FileName & "(" & j & ")" '<== versioning to differentiate the file
      FSO.MoveFile FileName, NewName '<== replace the old file
      FSO.MoveFile Source:=myPath & NewName, Destination:=ToDir '<== move to the destination folderEnd If

FileName = Dir()
Loop

Thanks in advance.
DZ
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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