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