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 ]
 

Some videos you may like

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

V_Malkoti

Well-known Member
Joined
Jun 10, 2015
Messages
898
Try changing:
Code:
Dim sh As Worksheet[COLOR=#ff0000]s[/COLOR]
to
Code:
Dim sh As Worksheet
 

Tetra201

MrExcel MVP
Joined
Oct 14, 2016
Messages
3,542
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:

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
12,697
Office Version
365, 2010
Platform
Windows, Mobile
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:
 

Watch MrExcel Video

Forum statistics

Threads
1,099,798
Messages
5,470,838
Members
406,730
Latest member
shubhangishastri

This Week's Hot Topics

Top