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
 

Some videos you may like

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,230
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,230
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,230
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:

Watch MrExcel Video

Forum statistics

Threads
1,123,281
Messages
5,600,715
Members
414,401
Latest member
grenona2020

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