VBA – Move Sheets to Closed Workbooks if Sheet Names Partially Match Closed Workbook Names

leever

New Member
Joined
Jun 29, 2023
Messages
9
Hello. I have 1 open workbook with multiple sheets.

Open Workbook Name: Source_Workbook
Source_Workbook
Sheets: [Economy_AA] [Economy_BB] [Economy_CC] [Economy_DD] [Economy_EE]

I also have 5 closed workbooks in a folder with the following file names and sheets:

"File_AA_Vancouver.xlsx" --> [Population_AA] [Weather_AA]
"File_BB_Toronto.xlsx" --> [Population_BB] [Weather_BB]
"File_CC_Calgary.xlsx" --> [Population_CC] [Weather_CC]
"File_DD_Ottawa.xlsx" --> [Population_DD] [Weather_DD]
"File_EE_Montreal.xlsx" --> [Population_EE] [Weather_EE]

I want to move each sheet in Source_Workbook to each of the files only if there is a partial match on sheet name and file name.

[Economy_AA] move to "File_AA_Vancouver.xlsx"
[Economy_BB] move to "File_BB_Toronto.xlsx"
[Economy_CC] move to "File_CC_Calgary.xlsx"
[Economy_DD] move to "File_DD_Ottawa.xlsx"
[Economy_EE] move to "File_EE_Montreal.xlsx"

I would also like the [Economy_##] sheet to go before the [Weather_##] sheet.

Is there a macro/code that can accomplish this instead of me moving each sheet one by one to the appropriate files. Any help would be greatly appreciated.

Thank you.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
This is the code I have written which works but is going to be repetitive when there are more sheets and files. I am looking for a dynamic way to get this done based on sheet name and file name. Thank you in advance.

---
Sub command()

Dim wb As Workbook

ThisWorkbook.Worksheets("Economy_AA").Select
ThisWorkbook.Worksheets("Economy_BB").Select
ThisWorkbook.Worksheets("Economy_CC").Select
ThisWorkbook.Worksheets("Economy_DD").Select
ThisWorkbook.Worksheets("Economy_EE").Select

Selection.Copy

Set wb = Workbooks.Open("C: My Folder\File_AA_Vancouver.xlsx")
ThisWorkbook.Worksheets("Economy_AA").Copy Before:=wb.Worksheets("Weather_AA")
wb.Close SaveChanges:=True
Set wb = Nothing

Set wb = Workbooks.Open("C: My Folder\File_BB_Toronto.xlsx")
ThisWorkbook.Worksheets("Economy_BB").Copy Before:=wb.Worksheets("Weather_BB")
wb.Close SaveChanges:=True
Set wb = Nothing

Set wb = Workbooks.Open("C: My Folder\File_CC_Calgary.xlsx")
ThisWorkbook.Worksheets("Economy_CC").Copy Before:=wb.Worksheets("Weather_CC")
wb.Close SaveChanges:=True
Set wb = Nothing

Set wb = Workbooks.Open("C: My Folder\File_DD_Ottawa.xlsx")
ThisWorkbook.Worksheets("Economy_DD").Copy Before:=wb.Worksheets("Weather_DD")
wb.Close SaveChanges:=True
Set wb = Nothing

Set wb = Workbooks.Open("C: My Folder\File_EE_Montreal.xlsx")
ThisWorkbook.Worksheets("Economy_EE").Copy Before:=wb.Worksheets("Weather_EE")
wb.Close SaveChanges:=True
Set wb = Nothing


End Sub
 
Upvote 0

Forum statistics

Threads
1,215,215
Messages
6,123,668
Members
449,114
Latest member
aides

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