VBA - reference a cell for sheet name for a macro

Brazzi079

New Member
Joined
Feb 9, 2016
Messages
1
Hi all - I'm trying to get a macro to copy sheets from other workbooks based on inputs in a "setup" tab. The sheet names are the same for each workbook, and I would like the VBA formula to reference a single cell input by the user.

For example, my current formula is:
Application.DisplayAlerts = False
Dim wkbk As Workbook
Set wkbk = ActiveWorkbook
WS_Count = ActiveWorkbook.Worksheets.Count
For j = 2 To WS_Count
ActiveWorkbook.Worksheets(WS_Count - j + 2).Delete
Next j

For i = 1 To 12
thepath = ActiveWorkbook.Sheets("Setup").Cells(i, 1).Value
Workbooks.Open Filename:=thepath, UpdateLinks:=0
Select Case i
Case 1
Sheets("XX").Copy After:=wkbk.Sheets(wkbk.Worksheets.Count)
Case 2
Sheets("XX").Copy After:=wkbk.Sheets(wkbk.Worksheets.Count)

I have 12 cases for monthly workbooks. In the last lines, "XX" is the tab name that I would like to have referenced to a cell in the active setup workbook that is receiving the copied sheets. How can I modify this to reference inputs in a cell? Bonus, if you know how to close the workbook that I currently have opening from the set file path inputs, that would be save me the second macro (which would also likely need to reference cells in my setup tab).

Thank you all in advance!
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Forum statistics

Threads
1,223,246
Messages
6,170,999
Members
452,373
Latest member
TimReeks

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