Runtime error 13 with vlookup in vba

bwar88

New Member
Joined
Feb 26, 2016
Messages
16
Hi

Any chance you could help me with one more thing...my code below keeps returning a runtime error 13 'type mismatch'. varX has been declared as a variant. Basically the error occurs when I (knowingly) try to do a vlookup on an empty cell. What's the best way to get around this - I just want to set varX = 0 if an error occurs?

Code:
For j = 1 To UBound(strTheBoats)
            varX = Application.IfError(Application.VLookup(Sheet2.Cells(j + 1, k + strTheBoats(i).MaxWait), Sheet1.Range("A:D"), 4, 0), 0)
            dblHrsTotal = dblHrsTotal + varX
            varX = Application.IfError(((Application.VLookup(Sheet2.Cells(j + 1, k + strTheBoats(i).MaxWait), Sheet1.Range("A:D"), 5, 0)) ^ 2), 0)
            dblHrsVarTotal = dblHrsVarTotal + varX
        Next j
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Does this work?
Code:
For j = 1 To UBound(strTheBoats)

            varX = Application.VLookup(Sheet2.Cells(j + 1, k + strTheBoats(i).MaxWait), Sheet1.Range("A:D"), 4, 0)
            If Not IsError(varX) Then
                dblHrsTotal = dblHrsTotal + varX
            End if

            varX = Application.VLookup(Sheet2.Cells(j + 1, k + strTheBoats(i).MaxWait), Sheet1.Range("A:D"), 5, 0)
            If Not IsError(varX) Then
                dblHrsVarTotal = dblHrsVarTotal + varX ^ 2
            End If
Next j
 
Upvote 0
Andrew

Where would the type mismatch happen in the code I posted?
 
Upvote 0
Well I think the type mismatch may be caused by this:

k + strTheBoats(i).MaxWait

As I said I don't get a type mismatch by trying to look up a blank cell with the original code. But, of course, I may be wrong.
 
Upvote 0
Andrew

I thought the error might have been with the ^2.

Whatever it is/was I think it's a good idea to separate this type of code out a bit to either fix it or help discover what the actual problem is.:)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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