I had an issue with this code before and when I tried fixing it another issue occurred. On this line of code,
If Not (IsError(Application.WorksheetFunction.VLookup(item, lookup_range, 2, False))) Then
I get the error unable to get the vlookup property of the worksheetfunction class"
The whole code is below. Thanks for any help!
Sub code()
Columns("E:G").Insert shift:=xlToRight, copyorigin:=xlFormatFromLeftOrAbove
Range("E6").value = "Drink Price"
Range("F6").value = "Drink Revenue"
Range("G6").value = "Gross Sales less Drink Revenue"
Dim i As Variant
Dim item As Variant
Dim lookup_range As Range
Dim rev_wksht As Worksheet
Dim rev_wbk As Workbook
Dim vlkup_wbk As Workbook
Set rev_wksht = ActiveWorkbook.Sheets(1)
Set vlkup_wbk = Workbooks.Open("C:\Users\user\Documents\vlookup table drink prices.xlsx")
Set lookup_range = vlkup_wbk.Worksheets("Sheet1").Range("A:B")
i = 7
Do While rev_wksht.Cells(i, 1).value <> ""
Set item = rev_wbk.Sheets(1).Cells(i, 1).value
If Not (IsError(Application.WorksheetFunction.VLookup(item, lookup_range, 2, False))) Then
rev_wksht.Cells(i, 5).value = Application.WorksheetFunction.VLookup(item, lookup_range, 2, False)
rev_wksht.Cells(i, 6).Formula = rev_wksht.Cells(i, 11).value * rev_wksht.Cells(i, 5).value
rev_wksht.Cells(i, 7).Formula = rev_wksht.Cells(i, 4).value - rev_wksht.Cells(i, 6).value
ElseIf (IsError(Application.WorksheetFunction.VLookup(item, lookup_range, 2, False))) Then
rev_wksht.Cells(i, 5).value = Empty
End If
i = i + 1
Loop
rev_wksht.Range("F:G").NumberFormat = "#,##0.00"
rev_wksht.Cells.EntireColumn.AutoFit
End Sub