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

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,344
Office Version
  1. 365
Platform
  1. Windows
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
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
What's strTheBoats(i).MaxWait? On a quick test looking up a blank cell didn't create a type mismatch.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,344
Office Version
  1. 365
Platform
  1. Windows
Andrew

Where would the type mismatch happen in the code I posted?
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
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.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,344
Office Version
  1. 365
Platform
  1. Windows
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:
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,987
Messages
5,834,723
Members
430,314
Latest member
Kym_beginner_in_VBA

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
Top