1
votes

I am getting a runtime 13 error type mismatch when I run this macro. The call out is "if... then".

The code seemingly works correctly. The only issue I can think of is that a seperate macro runs before this one that uses a formula to name cells(1,8) and then a copy, paste values occurs.

Sub NewWb()

Set wb1 = ThisWorkbook
Set wb2 = Workbooks.Add

For Each Worksheet In wb1.Worksheets
    **If Worksheet.Cells(1, 8).Value = "PI Fin Ops" Then**
        Worksheet.Move After:=wb2.Sheets(wb2.Sheets.Count)
    End If
Next Worksheet

End Sub

Any help is appreciated!

1

1 Answers

1
votes

That error would commonly raise if the cell contains an error value. You can easily trap that with the IsError function.

I would also recommend to avoid using reserved or semi-reserved keywords in your variable naming conventions, such as Worksheet.

Dim WS as Worksheet
Dim wb1 as Workbook
Dim wb2 as Workbook

Set wb1 = ThisWorkbook
Set wb2 = Workbooks.Add

For Each WS In wb1.Worksheets
    If Not IsError(WS.Cells(1,8).Value Then
        If WS.Cells(1, 8).Value = "PI Fin Ops" Then
            WS.Move After:=wb2.Sheets(wb2.Sheets.Count)
        End If
    End If

Next WS