runtime error 13 type mismatch in nested If statement

kwoifo

New Member
Joined
Apr 1, 2009
Messages
47
Hi,

I am encountering a runtime error 13 on the first If statement on the following code -- it seems to run through the loop and then produces the error midway through. I haven't been able to find a solution.. still newish at vba so would be grateful for any suggestions! Many thanks!
Code:
Sub test()
Dim x as Integer
Dim i as Long
Dim t as Integer

x = Application.WorksheetFunction.CountIf(Sheet1.Range("A1:AV500"), True)

For i = 1 To 500
    If Sheet1.Cells(i, 1) = True Then
    Sheet1.Cells(i, 4).Copy
        For t = 1 To x
            If Sheet2.Cells(t, 1).Value = vbNullString Then
            Sheet2.Cells(t, 1).PasteSpecial
            End If
        Next t
    End If
Next i
End Sub
Many thanks for any help!!
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi Andrew, thanks for taking a look! The cells of the first If contain a Boolean value. I tried using Cells(i,1).Value and get the same runtime error. The strange thing is it works for the first loop, then produces the error.
 
Upvote 0
Andrew -- thanks, one cell did indeed have an error. Removing the error makes the type mismatch go away.

I realize though now that my code only pastes the first "True" occurrence in the Sheet2 rows instead of pasting the next occurence in cells(t+1,1), cells(t+2,1) etc... that's something I need to fix!

Tks for the help!
 
Upvote 0
I was able to get my code to do what it's supposed to do now without the mismatch. Many thanks for your help Andrew! Sorry it was so simple!
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,431
Members
452,326
Latest member
johnshaji

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