Hello, I am teaching myself VBA (with a lot of help from MrExcel) and I am running into an issue when I attempt to expand my macro to more than one worksheet.
I have successfully tested the code below, which searches column A - and when it finds a number, in column A, it inserts that many rows below that cell. But, when I attempt to get this code to loop through all worksheets in the workbook, it doesn't work and delivers an error. I believe it is the structure/arraignment/order of my argument.
Also, if possible - I would like to adjust the macro so it doesn't start until row 2 (my worksheets have a "page number" in cell A1, so it shouldn't add rows after this page number in cell A1).
P.S. - sorry if my "indenting" is off. I do not completely understand this logic yet.
SUCCESSFUL MACRO:
Sub Insert()
Dim End_Row As Long, n As Long, Ins As Long
Dim sh As Worksheets
End_Row = Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
For n = End_Row To 1 Step -1
Ins = Cells(n, "A").Value
If Ins > 0 Then Range("A" & n + 1 & ":A" & n + Ins).EntireRow.Insert
Next n
End Sub
UNSUCCESSFUL ATTEMPT FOR ALL WORKSHEETS:
Sub Insert()
Dim End_Row As Long, n As Long, Ins As Long
Dim sh As Worksheets
For Each sh In ActiveWorkbook.Sheets
If Left(sh.Name, 9) = "Labor BOE" Then
End_Row = Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
For n = End_Row To 1 Step -1
Ins = Cells(n, "A").Value
If Ins > 0 Then Range("A" & n + 1 & ":A" & n + Ins).EntireRow.Insert
Next n
End If
Next sh
End Sub
BEFORE MACRO:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]1[/TD]
[TD]Name[/TD]
[TD]Hours[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Grand Total[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Name[/TD]
[TD]Hours[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Grand Total[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Name[/TD]
[TD]Hours[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Grand Total[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
AFTER MACRO:
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Name[/TD]
[TD]Hours[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Grand Total[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Name[/TD]
[TD]Hours[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Grand Total[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Name[/TD]
[TD]Hours[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Grand Total[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
I have successfully tested the code below, which searches column A - and when it finds a number, in column A, it inserts that many rows below that cell. But, when I attempt to get this code to loop through all worksheets in the workbook, it doesn't work and delivers an error. I believe it is the structure/arraignment/order of my argument.
Also, if possible - I would like to adjust the macro so it doesn't start until row 2 (my worksheets have a "page number" in cell A1, so it shouldn't add rows after this page number in cell A1).
P.S. - sorry if my "indenting" is off. I do not completely understand this logic yet.
SUCCESSFUL MACRO:
Sub Insert()
Dim End_Row As Long, n As Long, Ins As Long
Dim sh As Worksheets
End_Row = Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
For n = End_Row To 1 Step -1
Ins = Cells(n, "A").Value
If Ins > 0 Then Range("A" & n + 1 & ":A" & n + Ins).EntireRow.Insert
Next n
End Sub
UNSUCCESSFUL ATTEMPT FOR ALL WORKSHEETS:
Sub Insert()
Dim End_Row As Long, n As Long, Ins As Long
Dim sh As Worksheets
For Each sh In ActiveWorkbook.Sheets
If Left(sh.Name, 9) = "Labor BOE" Then
End_Row = Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
For n = End_Row To 1 Step -1
Ins = Cells(n, "A").Value
If Ins > 0 Then Range("A" & n + 1 & ":A" & n + Ins).EntireRow.Insert
Next n
End If
Next sh
End Sub
BEFORE MACRO:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]1[/TD]
[TD]Name[/TD]
[TD]Hours[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Grand Total[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Name[/TD]
[TD]Hours[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Grand Total[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Name[/TD]
[TD]Hours[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Grand Total[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
AFTER MACRO:
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Name[/TD]
[TD]Hours[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Grand Total[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Name[/TD]
[TD]Hours[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Grand Total[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Name[/TD]
[TD]Hours[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Grand Total[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]