I want to refer to newly added sheets. They are added by a loop.
They are named Sheet1
, Sheet2
, Sheet3
etc. I also have a variable n
which equals to 1 when a Sheet1
is created, equals to 2 when a Sheet2
is created, equals to 3 when a Sheet3
is created, and so on.
My code looks like the following:
For i = 2 To 1000
If Worksheets("DATA").Cells(i, "A").Value <> "" And Worksheets("DATA").Cells(i, "A").Value <> "TOTAL" Then
Worksheets("Template").Cells(1, "C").Value = Worksheets("DATA").Cells(i, "B").Value
Worksheets("Template").Cells(2, "C").Value = Worksheets("DATA").Cells(i, "C").Value
Worksheets("Template").Cells(3, "C").Value = Worksheets("DATA").Cells(i, "A").Value
Worksheets("Template").Cells(5, "D").Value = Worksheets("DATA").Cells(i, "D").Value
Worksheets("Template").Cells(5, "E").Value = Worksheets("DATA").Cells(i, "E").Value
Sheets.Add
n = 0
n = n + 1
Worksheets("Template").Cells.Copy
Worksheets("Sheet & n").Range("A1").Paste 'Problem is in this line
Worksheets("Sheet" & n).Activate ' And in this line
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
I have a problem with the lines marked with comments.
My question is how to refer to a Sheet1
, Sheet2
, etc by using the n
variable?
Please be patient with me, as I am quite new to VBA. If my question is vague or hard to understand please let me know.
Thank you in advance.