I have this pivot table script that was working fine in another workbook.
But then I needed it to run only on the last sheet of another workbook. So I took out "For each ws in worksheets" now I am getting an error "object variable or with block not set"
Not sure what happend?
Any Idea's?
Here is the code, Thanks for helping, Mike
Sub Pivot2()
Dim Ws As Worksheet, strSource As String
Dim PT As PivotTable, pc As PivotCache, pf As PivotField
With ActiveWorkbook.Worksheets(Sheets.Count)
'For Each Ws In ActiveWorkbook.Worksheets
ActiveWorkbook.ShowPivotTableFieldList = False
Application.CommandBars("PivotTable").Visible = False
Application.DisplayAlerts = False
Application.ScreenUpdating = False
strSource = Ws.Name & "!R1C1:R5000C4"
ActiveWorkbook.Worksheets.Add 'new worksheet becomes the active sheet
'ActiveSheet.Name = "Info"
Set pc = ActiveWorkbook.PivotCaches.Add(xlDatabase, strSource)
Set PT = pc.CreatePivotTable(Range("A1")) 'pivot table at A1 on new sheet
Set pf = PT.PivotFields("Order Number")
PT.AddDataField pf, "Count of Order Number", xlCount
PT.AddFields RowFields:="Name"
Application.DisplayAlerts = True
Application.ScreenUpdating = True
'Next Ws
End With
End Sub
Bookmarks