Pass variables into vlookup worksheet function in VBA Sub

AlexB123

Board Regular
Joined
Dec 19, 2014
Messages
207
Hi,

I have a work project that uses dates of a broadcast calendar to determine month and quarter throughout the year. My solution was to hardcode the data into a spreadsheet within the macro workbook, and then use functions to get the data I need to drive my macro. However, I keep receiving "Run-time error '1004': Unable to get the VLookup property of the WorksheetFunction class." The values (today's date, last friday, etc are in the table), which looks like:

DateYearWeekNumMonthname
12/27/2120221January
12/28/2120221January
12/29/2120221January
12/30/2120221January
12/31/2120221January
01/01/2220221January
01/02/2220221January
01/03/2220222January

Here is the snippet of my VBA where the error is occuring:
VBA Code:
'Bind macro workbook Object
        Set macroWB = ThisWorkbook
            Set macroWS = macroWB.Worksheets("BROADCAST_MONTH_1")
    
    'Find current date and Broadcast Week information
                    Dim ct_DATE As Date: ct_DATE = Format(Date, "mm/dd/yy")
                    Dim iWeekday As Integer: iWeekday = Weekday(Now(), vbFriday)
                    Dim LastFridayDate As Date: LastFridayDate = Format(Now - (iWeekday - 1), "mm.dd.yy")
                    Dim weekInt As String: weekInt = WorksheetFunction.VLookup(ct_DATE, macroWS.Range("A:E"), 3, 0)
                    Dim lastFriWeekInt As String: lastFriWeekInt = Application.WorksheetFunction.VLookup(LastFridayDate, macroWS.Range("A:E"), 3, 0)
                    Dim monthName As String: monthName = Application.WorksheetFunction.VLookup(ct_DATE, macroWS.Range("A:E"), 4, 0)
                    Dim lastFriMonthName As String: lastFriMonthName = Application.WorksheetFunction.VLookup(LastFridayDate, macroWS.Range("A:E"), 4, 0)
                    Dim qtrName As String: qtrName = Application.WorksheetFunction.VLookup(ct_DATE, macroWS.Range("A:E"), 5, 0)
                    Dim lastFriQtrName As String: lastFriQtrName = Application.WorksheetFunction.VLookup(LastFridayDate, macroWS.Range("A:E"), 5, 0)

I have tested all of the attempts to use VLookup in the above lines. All fail with the same error.

Thanks for any help or suggestions!
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
It's a tricky error. Case to specify the date as a search value, Date type cannot be used, it must be a Long type for "WorksheetFunction.VLookup". I had struggled with the same issue a long time ago. Even now, I have no idea how come it only accepts a Long type. But just take it as "WorksheetFunction.VLookup" is made like this. For other WorksheetFunctions, the Date type works without any problems.

So just change the declaration of the variable ct_DATE as follows. Hope this helps.
VBA Code:
Dim ct_DATE As Long: ct_DATE = Date
 
Upvote 0
Oh, other Date type variables must be changed as well.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,559
Members
449,089
Latest member
Motoracer88

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