Loop through multiple sheets - Run-time error '13': Type mismatch

jwb1012

Board Regular
Joined
Oct 17, 2016
Messages
167
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 ]
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try changing:
Code:
Dim sh As Worksheet[COLOR=#ff0000]s[/COLOR]
to
Code:
Dim sh As Worksheet
 
Upvote 0
Code:
 Sub Insert()
 Dim End_Row As Long, n As Long
[COLOR=#ff0000] Dim Ins As Variant[/COLOR]
 Dim sh As [COLOR=#ff0000]Worksheet[/COLOR]
 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,[COLOR=#FF0000] 1[/COLOR]).Value

 If[COLOR=#ff0000] IsNumeric(Ins) And [/COLOR](Ins > 0) Then sh.Range("A" & n + 1 & ":A" & n + Ins).EntireRow.Insert
 Next n

 End If
 Next sh
 End Sub
 
Last edited:
Upvote 0
Code:
 [COLOR=#ff0000] 
Ins = sh.Cells(n,[COLOR=#FF0000] 1[/COLOR]).Value
[/QUOTE]

Why change the "A" to 1? using a column letter inside quotes is legitimate syntax inside Cells :confused:
 
Upvote 0

Forum statistics

Threads
1,213,506
Messages
6,114,025
Members
448,543
Latest member
MartinLarkin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top