Error 91 using Function related to vlookup

03856me

Active Member
Joined
Apr 4, 2008
Messages
297
I have the following code - it worked perfectly the first time I used it, but not again. I am getting Run-time error '91' now, Object variable or With block variable not set, on the row of code I have highlighted - any ideas? I have researched and can't figure out how to fix it.

Code:
Sub PTOlookup()
     OptimizeVBA True
     
     Dim sWb As Workbook
     Dim fWs As Worksheet, sWs As Worksheet
     Dim slRow As Long, flRow As Long
     Dim pEMP As Range, luVal As Range
     Dim lupEMP As Range, outputCol As Range
     Dim vlookupCol As Object
     
     Dim book2Name As String
        book2Name = "PTO Available-Co 50.xlsx"
     Dim book2NamePath
        book2NamePath = ThisWorkbook.Path & "\" & book2Name
     
     If IsOpen(book2Name) = False Then Workbooks.Open (book2NamePath)
     Set sWb = Workbooks(book2Name)
     Set sWs = sWb.Sheets("Page1_1")
     Set fWs = ThisWorkbook.Sheets("Summary")
     
     slRow = sWs.Cells(Rows.Count, 3).End(xlUp).Row
     flRow = fWs.Cells(Rows.Count, 5).End(xlUp).Row
     
     Set pEMP = sWs.Range("a3:a" & slRow)
     Set lupEMP = fWs.Range("b5:b" & flRow)
     For i = 29 To 29
         Set outputCol = fWs.Range(fWs.Cells(5, i), fWs.Cells(flRow, i))
         Select Case i
             Case 28
                 Set luVal = sWs.Range("b3:b" & slRow)
            
         End Select
     'Build Collection
       Set vlookupCol = BuildLookupCollection(pEMP, luVal)
     'Lookup the values
       VLookupValues lupEMP, outputCol, vlookupCol
     Next i
     
     OptimizeVBA False
     
     sWb.Close False
     Set vlookupCol = Nothing
End Sub

Function BuildLookupCollection(categories As Range, values As Range)
     Dim vlookupCol As Object, i As Long
     Set vlookupCol = CreateObject("Scripting.Dictionary")
     For i = 1 To categories.Rows.Count
         [COLOR=#ff0000]vlookupCol.Item(CStr(categories(i))) = values(i)[/COLOR]
     Next i
     Set BuildLookupCollection = vlookupCol
End Function

Sub VLookupValues(lookupCategory As Range, lookupValues As Range, vlookupCol As Object)
     Dim i As Long, resArr() As Variant
     ReDim resArr(lookupCategory.Rows.Count, 1)
     For i = 1 To lookupCategory.Rows.Count
         resArr(i - 1, 0) = vlookupCol.Item(CStr(lookupCategory(i)))
     Next i
     lookupValues = resArr
End Sub

Sub OptimizeVBA(isOn As Boolean)
     Application.EnableEvents = Not (isOn)
     Application.ScreenUpdating = Not (isOn)
     ActiveSheet.DisplayPageBreaks = Not (isOn)
End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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