VBA Vlookup Function

mikeymay

Well-known Member
Joined
Jan 17, 2006
Messages
1,598
Office Version
  1. 365
Platform
  1. Windows
I am attempting to recreate the following Excel function which returns the expected result
Code:
=VLOOKUP(B8,Rates_Lookup,3,FALSE)

The code I am using to recreate this is
Code:
.Offset(lngCount, intLoans * 2) = Application.WorksheetFunction.VLookup(dtStart + lngCount, rngRatesLookup, 3, False)
but I get a 'Unable to get the Vlookup property of the WorksheetFunctionclass'

In the Excel formula B8 = 10 Jun 19 in a date format
In the VBA code dtStart + lngCount returns the same in a date format (dtStart is declared as a date variable)

In the Excel formula 'Rates_Lookup' is the correct range
In the VBA code rngRatesLookup is based on the same range

I have used this VBA function before but I just can't see what is wrong with the VBA version.


TIA
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
The mistake is because you are not finding the date.
In the excel dates are stored as a number, so you can use the CDbl function to change the date to the excel number.


However, if the date does not exist, send an error.
To prevent the code from stopping by error, you must use On Error.


Code:
[COLOR=#008000]        [/COLOR][COLOR=#ff0000]On Error Resume Next[/COLOR][COLOR=#008000]
[/COLOR]        Dim res As Variant

        res = Application.WorksheetFunction.VLookup([COLOR=#0000ff]CDbl[/COLOR](dtStart + lngCount), rngRatesLookup, 3, False)
        If Err.Number = 0 Then
            .Offset(lngCount, intLoans * 2) = res
        Else
            MsgBox "date does not exists"
        End If

--------------------------------------------
Although it is not good practice to use the On Error statement. I recommend using the Find method


Note: The date format on your sheet should be in "dd/mm/yyyy" or "mm/dd/yyyy" depending on the format you use.
Try this:
Code:
        Dim f As Range
        Set f = rngRatesLookup.Find(dtStart + lngCount, LookIn:=xlValues, lookat:=xlWhole)
        If Not f Is Nothing Then
            .Offset(lngCount, intLoans * 2) = f.Offset(0, 2).Value
        Else
            MsgBox "Dont exists"
        End If
 
Upvote 0

Forum statistics

Threads
1,213,485
Messages
6,113,931
Members
448,533
Latest member
thietbibeboiwasaco

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