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 ]
 

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,471
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
11,480
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:
 

Forum statistics

Threads
1,081,730
Messages
5,360,934
Members
400,602
Latest member
newaqua

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top