Shorten Excel VBA Code with Worksheet Loops and Dynamic References
Question details
Replace repeated VBA code for 18 worksheets with loops that generate worksheet names, source columns, and formulas.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- A workbook needs formulas written to the same cells on 18 numbered worksheets using a repeating sequence of source columns.
- Observed behavior
- The desired macro writes formulas to K2:K7 on each numbered sheet, referencing rows 3:8 on the BYE WEEKS sheet.
In Excel, confirm that the workbook contains sheets named 1 through 18 and a source sheet named BYE WEEKS. The example overwrites cells K2:K7 on the numbered sheets, so check that these are the intended destination cells.
Loop through the numbered worksheets and build formulas
Use one worksheet loop and one row loop to replace repeated code when the source columns follow the B, E, H, K, N, Q cycle.
The worksheet loop selects sheets 1 through 18. The Mod calculation repeats the six source columns every six sheets, while the row loop writes formulas to K2:K7 using source rows 3:8.
In desktop Excel, open Developer > Visual Basic. In Project Explorer, open the worksheet code module containing the existing CommandButton1_Click procedure.
Replace the existing CommandButton1_Click procedure with this code; do not leave a second procedure with the same name: Private Sub CommandButton1_Click() Dim w As Long Dim sh As Worksheet Dim r As Long Dim c As String For w = 1 To 18 Set sh = ThisWorkbook.Worksheets(CStr(w)) c = Chr(66 + 3 * ((w - 1) Mod 6)) For r = 2 To 7 sh.Range("K" & r).Formula = "=IF(INDIRECT(""'BYE WEEKS'!" & c & (r + 1) & """)<>"""",INDIRECT(""'BYE WEEKS'!" & c & (r + 1) & """),"""")" Next r Next w End Sub
Save the workbook as an Excel Macro-Enabled Workbook (.xlsm). Enable macros if you trust the file, then click CommandButton1 on its worksheet and inspect cells K2:K7 on a few numbered sheets.

Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for Excel formats, making it easy to move everyday spreadsheet work. VBA support varies by edition and environment, so verify macro compatibility before relying on this Excel procedure.

Frequently Asked Questions
How does the macro choose the source column for each worksheet?
The expression Chr(66 + 3 * ((w - 1) Mod 6)) produces B, E, H, K, N, and Q, then repeats the sequence for the next six numbered sheets.
Which cells does the macro overwrite?
It writes formulas to K2:K7 on sheets named 1 through 18. Check those cells before clicking CommandButton1.
Why does the macro report a subscript out of range error?
In Excel, check the sheet tabs for worksheets named exactly 1 through 18 in the workbook containing the macro. ThisWorkbook.Worksheets(CStr(w)) cannot find a sheet if its name differs.
Why do the formulas return a reference error?
Check that the source worksheet tab is named BYE WEEKS. The formulas use INDIRECT to reference that sheet and the calculated source cell.
How can I keep the VBA code when saving the workbook?
In Excel, select File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). Enable macros only when you trust the workbook.




