Hello,
I created a macro that uses a counter to create 3 worksheets for each country included in a data tab. The data tab can have a variable number of countries, so the macro counts the number of countries and creates a Cover Sheet, Expense Sheet, and Income Sheet for each country (e.g. the resulting worksheet names would be Cover Sheet 1, Expenses 1, Income 1, Cover Sheet 2, Expenses 2, Income 2, etc). Below is a condensed summary of the pertinent macro.
Sub CreateCountryWorksheets
Dim ws As Worksheet, wsTemplate As Worksheet
Dim n As Integer, i As Long
n = ws.Range("D1").Value
If n > 0 Then
For i = 1 To n
wsCover.Copy after:=Sheets(Sheets.Count)
ActiveSheet.Name = "Cover Sheet " & i
‘Bunch of stuff
wsExpense.Copy after:=Sheets(Sheets.Count)
ActiveSheet.Name = "Expenses " & i
‘Bunch of stuff
wsIncome.Copy after:=Sheets(Sheets.Count)
ActiveSheet.Name = "Income " & i
‘Bunch of stuff
Next i
End If
‘Other stuff
End Sub
The macro is working fine, but now I received a request to keep the workbook intact with all the countries and all three tabs, but also create a new workbook for each country with just the three tabs pertinent to each country. Each new workbook would need to be saved in the same path.
I think I need to use loops, but I can’t figure out whether to 1) include a Save As in the loops that I already have or 2) or create all the worksheets first then find all 3 sheet names which contain the same number (1 to i) to then Save As. Either way, I am not sure of the code to use. I can manage with loops to find one sheet name, but finding 3 sheets and Saving As is throwing me.
I am fairly familiar with VBA, but specific code would be very helpful.
Thank you so much.
I created a macro that uses a counter to create 3 worksheets for each country included in a data tab. The data tab can have a variable number of countries, so the macro counts the number of countries and creates a Cover Sheet, Expense Sheet, and Income Sheet for each country (e.g. the resulting worksheet names would be Cover Sheet 1, Expenses 1, Income 1, Cover Sheet 2, Expenses 2, Income 2, etc). Below is a condensed summary of the pertinent macro.
Sub CreateCountryWorksheets
Dim ws As Worksheet, wsTemplate As Worksheet
Dim n As Integer, i As Long
n = ws.Range("D1").Value
If n > 0 Then
For i = 1 To n
wsCover.Copy after:=Sheets(Sheets.Count)
ActiveSheet.Name = "Cover Sheet " & i
‘Bunch of stuff
wsExpense.Copy after:=Sheets(Sheets.Count)
ActiveSheet.Name = "Expenses " & i
‘Bunch of stuff
wsIncome.Copy after:=Sheets(Sheets.Count)
ActiveSheet.Name = "Income " & i
‘Bunch of stuff
Next i
End If
‘Other stuff
End Sub
The macro is working fine, but now I received a request to keep the workbook intact with all the countries and all three tabs, but also create a new workbook for each country with just the three tabs pertinent to each country. Each new workbook would need to be saved in the same path.
I think I need to use loops, but I can’t figure out whether to 1) include a Save As in the loops that I already have or 2) or create all the worksheets first then find all 3 sheet names which contain the same number (1 to i) to then Save As. Either way, I am not sure of the code to use. I can manage with loops to find one sheet name, but finding 3 sheets and Saving As is throwing me.
I am fairly familiar with VBA, but specific code would be very helpful.
Thank you so much.