Error 1004

vmjan02

Well-known Member
Joined
Aug 15, 2012
Messages
1,057
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
I have mention code and it creates pivot table, but concern is it gives error as 1004, I am using excel 2016. Any Idea.

Code:-
Private Sub CommandButton1_Click()
'Sheets("AG1 Node Down").Select
Dim Pt As PivotTable
Dim PtCache As PivotCache
Dim pageField1 As String
Dim pageField2 As String
Dim pageField3 As String
Dim rowField1 As String
Dim rowField2 As String
Dim colField As String
Dim dataField As String


On Error Resume Next
Application.DisplayAlerts = False
Sheets("Repeat Cnt SAP ID WK33").Delete
On Error GoTo 0
Application.DisplayAlerts = True


Set s = Sheets("Sheet2")
With Worksheets.Add
.Name = "Repeat Cnt SAP ID WK33"
End With


pageField1 = s.Cells(1, 2).Value
rowField1 = s.Cells(1, 3).Value


colField1 = s.Cells(1, 10).Value
dataField = s.Cells(1, 11).Value


Worksheets("AG1 Node Down").Activate
ActiveSheet.Range("C5").Select
ActiveSheet.Range(Selection, Selection.End(xlToRight)).Select
ActiveSheet.Range(Selection, Selection.End(xlDown)).Select
Selection.Name = "Vir"

' Error here 1004--- it is not creating pivot table '
Set PtCache = ActiveWorkbook.PivotCaches.Add( _
SourceType:=xlDatabase, _
SourceData:=s.Range("Vir"))


Set Pt = PtCache.CreatePivotTable( _
TableDestination:=Sheets("Repeat Cnt SAP ID WK33").Range("A3"), _
TableName:="Q1")




End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Any Idea.

At a guess I'd say there's something wrong with the Vir named range. Check it exists and that it's been set properly via Name Manger (accessed by pressing Ctrl + F3 i.e. press and hold Ctrl and then press F3).

Robert
 
Upvote 0
Yes "Vir" name range is properly set in Name Manager.
 
Last edited:
Upvote 0
Not sure then as it worked for me :confused:
Make sure Sheet2 is not protected is something else you can try.
 
Upvote 0
Actually when you set the "Vir" named range it's based on the "AG1 Node Down" tab yet in your code you're trying to associate the "Vir" named range with "Sheet2" i.e. s.Range("Vir")) which is causing the issue. Remove the "s." from this line of code that's failing...

Rich (BB code):
Set PtCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=s.Range("Vir"))

...so it's like this and all should be good:

Rich (BB code):
Set PtCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=Range("Vir"))

Robert
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,525
Members
448,969
Latest member
mirek8991

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