shane_kidani
New Member
I have this code used for my macro:
which works fine, except for one big irritation: I don't want to repeat the steps 46 times.
Can anyone out there suggest a way to condense all this into one step?
Here is the goal of the workbook:
1. Run the macro "CreateNextWeek"
2. The macro will prompt for the number of the new week
3. After entering the number and clicking OK, a new sheet is created
4. As the new sheet is created, D2 will be changed to the value of the previous sheet's H2
5. Repeat automatically down to D46
The problem is that for each row, I must select the workbook where the sheets are contained. I have uploaded the file for review. Thank you.
Code:
Option Explicit
Sub CreateNextWeek()
Dim WeekNbr As Long
WeekNbr = InputBox("Enter the week number.")
Sheets("Template").Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = "WE (" & WeekNbr & ")"
Cells(2, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H2"
Cells(3, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H3"
Cells(4, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H4"
Cells(5, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H5"
Cells(6, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H6"
Cells(7, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H7"
Cells(8, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H8"
Cells(9, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H9"
Cells(10, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H10"
Cells(11, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H11"
Cells(12, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H12"
Cells(13, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H13"
Cells(14, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H14"
Cells(15, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H15"
Cells(16, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H16"
Cells(17, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H17"
Cells(18, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H18"
Cells(19, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H19"
Cells(20, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H20"
Cells(21, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H21"
Cells(22, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H22"
Cells(23, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H23"
Cells(24, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H24"
Cells(25, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H25"
Cells(26, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H26"
Cells(27, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H27"
Cells(28, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H28"
Cells(29, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H29"
Cells(30, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H30"
Cells(31, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H31"
Cells(32, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H32"
Cells(33, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H33"
Cells(34, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H34"
Cells(35, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H35"
Cells(36, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H36"
Cells(37, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H37"
Cells(38, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H38"
Cells(39, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H39"
Cells(40, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H40"
Cells(41, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H41"
Cells(42, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H42"
Cells(43, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H43"
Cells(44, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H44"
Cells(45, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H45"
Cells(46, "D").Formula = "='Week (" & WeekNbr - 1 & ")'!H46"
End Sub
which works fine, except for one big irritation: I don't want to repeat the steps 46 times.
Can anyone out there suggest a way to condense all this into one step?
Here is the goal of the workbook:
1. Run the macro "CreateNextWeek"
2. The macro will prompt for the number of the new week
3. After entering the number and clicking OK, a new sheet is created
4. As the new sheet is created, D2 will be changed to the value of the previous sheet's H2
5. Repeat automatically down to D46
The problem is that for each row, I must select the workbook where the sheets are contained. I have uploaded the file for review. Thank you.