VBA Error Run-Time 91 - object variable or with block variable not set (Code included)

LNG2013

Active Member
Joined
May 23, 2011
Messages
466
When I am trying to run the following code I am getting an error of:
Run-Time error '91':

Object variable or With block variable not set

VBA Code:
Sub CreatePivot()


Dim nr As Range
Dim pt As PivotTable
Dim ws As Worksheet
Dim LastRow As Long




For Each ws In Worksheets
    Select Case ws.Name
        Case "Data0", "Data1", "Data2", "Data3", "Data4", "Data5"
 
            With ws


     LastRow = .Range("N" & .Rows.Count).End(xlUp).Row
          nr = ws.Range("A2" & ":O" & LastRow)
         
         
        ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
            ws.Name & nr, Version:=3) _
            .CreatePivotTable TableDestination:=ws.Name & "A" & LastRow + 10, _
            TableName:="PivotTable4", DefaultVersion:=xlPivotTableVersion14
        Set pt = ws.PivotTables("PivotTable4")
        pt.AddDataField pt.PivotFields("Code"), "Sum of Code", xlSum
        With pt.PivotFields("SessionDate")
            .Orientation = xlRowField
            .Position = 1
        End With
       
        With pt.PivotFields("Session")
            .Orientation = xlRowField
            .Position = 2
        End With
       
        With pt.PivotFields("Probe#")
            .Orientation = xlRowField
            .Position = 3
        End With
       
     
    End With

        Case Else
            'Do Nothing
    End Select
Next ws

End Sub
 

Attachments

  • 2022-10-07 13_38_09-Microsoft Excel - Report.xlsm.png
    2022-10-07 13_38_09-Microsoft Excel - Report.xlsm.png
    4.2 KB · Views: 11

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try
Rich (BB code):
Set nr = ws.Range("A2:O" & LastRow)

but looking at the rest of your code you have other issues after that line
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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