I'm currently running into an issue where I have some logic in excel VBA that allows me to skip parts of a function. The issue is, it works in some areas, but does not work in other areas. I have 3 skippable areas, and the 1st two work, but the last does not appear to. I am currently getting a Run-Time Error 91: Object Variable or With Block variable not set, but I very clearly set the variable before the code section begins.
I've tried hard-coding the variable, and using separate variables, but unfortunately, nothing seems to work for this last piece.
Sub Tester()
'FIRST PART - BUILD HIGH LEVEL METRICS
Application.DisplayAlerts = False
Dim tSheet, sARR As Worksheet
Dim dateSel, rFinder, rFinderTemp As Variant
Dim pTable As PivotTable
Dim pRange As Range
Dim lastRow, lastCol, i, j, rnum As Long
Dim pAddy As String
dateSel = "11/17/2019"
Application.DisplayAlerts = False
Sheets.Add before:=ActiveSheet
ActiveSheet.Name = "TempTable"
Set tSheet = Worksheets("TempTable")
Set sARR = Worksheets("All_Risk_Report")
lastRow = sARR.Cells(Rows.Count, 1).End(xlUp).Row
lastCol = sARR.Cells(1, Columns.Count).End(xlToLeft).Column
Set pRange = sARR.Cells(1, 1).Resize(lastRow, lastCol)
pAddy = sARR.Name & "!" & pRange.address
Set pTable = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=pAddy).CreatePivotTable( _
TableDestination:=tSheet.Cells(2, 2))
With pTable
.PivotFields("GROUPDATE").Orientation = xlPageField
.PivotFields("GROUPDATE").CurrentPage = dateSel
.PivotFields("CRL").Orientation = xlColumnField
.PivotFields("Org_Category").Orientation = xlRowField
.PivotFields("Change_Request").Orientation = xlDataField
End With
With pTable.PivotFields("Sum of Change_Request")
.Function = xlCount
End With
Sheets("TempTable").Activate
ActiveSheet.Cells(1, 1).Select
rFinder = 0
On Error GoTo Skipper1
Let rFinder = Sheets("TempTable").Cells.Find("Enterprise").Row
If rFinder > 0 Then
Sheets("TempTable").Range("C" & rFinder & ":H" & rFinder).Select
Selection.Copy
Sheets("Calendar").Activate
ActiveSheet.Range("K4:P4").Select
Selection.PasteSpecial Paste:=xlPasteValues
rFinderTemp = rFinder
End If
Skipper1:
Sheets("TempTable").Activate
ActiveSheet.Cells(1, 1).Select
rFinder = 0
On Error GoTo Skipper2
Let rFinder = Sheets("TempTable").Cells.Find("Home Office").Row
If rFinder > 0 Then
Sheets("TempTable").Range("C" & rFinder & ":H" & rFinder).Select
Selection.Copy
Sheets("Calendar").Activate
ActiveSheet.Range("K5:P5").Select
Selection.PasteSpecial Paste:=xlPasteValues
rFinderTemp = rFinder
End If
Skipper2:
Sheets("TempTable").Activate
ActiveSheet.Cells(1, 1).Select
rFinder = 0
On Error GoTo Skipper3
Let rFinder = Sheets("TempTable").Cells.Find("WIMT").Row
If rFinder > 0 Then
Sheets("TempTable").Range("C" & rFinder & ":H" & rFinder).Select
Selection.Copy
Sheets("Calendar").Activate
ActiveSheet.Range("K6:P6").Select
Selection.PasteSpecial Paste:=xlPasteValues
rFinderTemp = rFinder
End If
Skipper3:
Sheets("TempTable").Activate
ActiveSheet.Cells(1, 1).Select
rnum = rFinder + 1
If rFinderTemp > rFinder Then
rnum = rFinderTemp + 1
End If
Sheets("TempTable").Range("C" & rnum & ":H" & rnum).Select
Selection.Copy
Sheets("Calendar").Activate
ActiveSheet.Range("K7:P7").Select
Selection.PasteSpecial Paste:=xlPasteValues
With Sheets("Calendar")
For i = 4 To 7
For j = 11 To 16
Cells(i, j).Select
If Selection.Value = "" Then
Selection.Value = 0
End If
Next
Next
End With
Sheets("TempTable").Activate
ActiveSheet.Delete
Sheets("Calendar").Activate
End Sub
My expected result is that each section will kick off, if it is needed, and that the copying rows will be copied over correctly. When the code runs properly (IE: The value the find function is looking for is actually there) it works just fine, but when the value isn't there, it craps out on me.
Any help or advice you could give would be greatly appreciated. Thanks in advance!