Home » excel » vba – Run-time Error '13' Type Mismatch IF value = Then Statement loop

vba – Run-time Error '13' Type Mismatch IF value = Then Statement loop

Posted by: admin May 14, 2020 Leave a comment

Questions:

Basically what I’m doing is I have data in columns A-I and I’m trying to copy out data [Columns D-G] by their value in D. In this loop I’m looking for when the entry in D says “1CME Cash-Settled Butter”.

I then starting in column K (=11) I paste it. Then I stack the rows.

The loop starts over and starts looking for a different value.

My problem is that I’ve ran the code fine for many runs. I haven’t changed the code since, but now I’m getting Run-Time Error Type ’13’ Mismatch. Is there someway to fix this or write new code?

I’m just learning VBA!

Thanks!

iRow = 2
For Each i In Range("D2:D200")
    If i.Value = "1CME Cash-Settled Butter" Then
        Range(i.Offset(0, 0), i.Offset(0, 3)).Copy
        Cells(iRow, 11).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,   SkipBlanks:=False, Transpose:=False
        iRow = iRow + 1
    End If
Next i

iRow = 30
For Each i In Range("D2:D200")
    If i.Value = "-1CME Cash-Settled Butter" Then
        Range(i.Offset(0, 0), i.Offset(0, 3)).Copy
        Cells(iRow, 11).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        iRow = iRow + 1
    End If
Next i
How to&Answers:

Change i.Value to i.Text. You don’t want the Value, because you’re comparing it to an integer. You want to check the String, and using i.Text will do that.

You can also cstr(i.Value), but that’s not the best way to do it.