Hello, I am receiving a "Run-time error '13': Type mismatch" when I adjusted my code to loop through all worksheets beginning with "Labor BOE" instead of just the specific worksheet I was working on. Does anyone have any thoughts on why this is occurring and how to fix it?
The goal of the code is to go through all worksheets where the name begins with "Labor BOE," look in column A (beginning in row 2), find all cells with a number, and then insert that many rows below that cell.
[ c o d e ]
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 = sh.Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
For n = End_Row To 2 Step -1
Ins = sh.Cells(n, "A").Value
If Ins > 0 Then sh.Range("A" & n + 1 & ":A" & n + Ins).EntireRow.Insert
Next n
End If
Next sh
End Sub
[ \ c o d e ]
My previous code only worked on an individual sheet, but did not go through all sheets beginning with "Labor BOE":
[ c o d e ]
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 2 Step -1
Ins = Cells(n, "A").Value
If Ins > 0 Then Range("A" & n + 1 & ":A" & n + Ins).EntireRow.Insert
Next n
End Sub
[ \ c o d e ]
The goal of the code is to go through all worksheets where the name begins with "Labor BOE," look in column A (beginning in row 2), find all cells with a number, and then insert that many rows below that cell.
[ c o d e ]
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 = sh.Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
For n = End_Row To 2 Step -1
Ins = sh.Cells(n, "A").Value
If Ins > 0 Then sh.Range("A" & n + 1 & ":A" & n + Ins).EntireRow.Insert
Next n
End If
Next sh
End Sub
[ \ c o d e ]
My previous code only worked on an individual sheet, but did not go through all sheets beginning with "Labor BOE":
[ c o d e ]
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 2 Step -1
Ins = Cells(n, "A").Value
If Ins > 0 Then Range("A" & n + 1 & ":A" & n + Ins).EntireRow.Insert
Next n
End Sub
[ \ c o d e ]