I have vba code that creates a pivot table automatically from some data that is copied from another workbook to the current workbook. When I run it on my machine, it is fine. Another person has no problems, either, but one woman is getting an error about "Run-time error '5': Invalid procedure call or argument" and it highlights my code for
"ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"WorkRange", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:=ActiveSheet.Cells(3,1), TableName:="PivotTable2", DefaultVersion _
:=xlPivotTableVersion14
My code copies the new data sheet and renames it every time to "Call Data - Date Time" and creates a new pivot table on a new sheet every time, so I don't think it is the table name causing issues, especially since it runs fine for me. She is the only one having issues. Any ideas?
Here is all of the code:
Sub GeneratePivot()
'
' Macro11 Macro
'
Dim myDate As Date, aDate
myDate = Date + 7 - Weekday(Date)
aDate = Format(myDate, "mm.dd.yyyy")
Dim LValue As Date
LValue = Now
Range("A1").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
ActiveWorkbook.Names.Add Name:="WorkRange", RefersTo:=Selection
Range("C5").Select
Application.Goto Reference:="WorkRange"
Sheets.Add
ActiveSheet.Name = "SummaryData " & Format(DateTime.Now, "MM.dd.yy hh.mm.ss")
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"WorkRange", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:=ActiveSheet.Cells(3, 1), TableName:="PivotTable2", DefaultVersion _
:=xlPivotTableVersion14
ActiveSheet.Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Month")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Site/Location")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Called Number")
.Orientation = xlRowField
End With
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
"PivotTable2").PivotFields("Called Number"), "Count of Called Number", xlCount
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Site/Location")
.PivotItems("#N/A").Visible = False
End With
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub
So I updated the suspect lines to:
Dim PC As PivotCache
Dim pt As PivotTable
Set PC = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"WorkRange", Version:=xlPivotTableVersionCurrent)
Set pt = PTCache.CreatePivotTable _
(TableDestination:=ActiveSheet.Cells(3, 1), _
TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersionCurrent)
PC.CreatePivotTable TableDestination:=ActiveSheet.Cells(3, 1), TableName:="PivotTable1", DefaultVersion _ :=xlPivotTableVersionCurrent
And it failes on Set PC. I also tried xlPivotTableVersion15, but that failed, as well.
I think the problem is because of the version of the PivotTable namely this section: Version:=xlPivotTableVersion14
Version14 is for Excel 2010. I'm not sure what the version number is for 2012.
Try changing it to:
DefaultVersion:=xlPivotTableVersionCurrent and see if it runs.
Here is a list (taken from here: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.xlpivottableversionlist(v=office.15).aspx) with the available versions. I don't see Excel 2012 on there, so your mileage may vary:
xlPivotTableVersion2000 refers to Excel 2000
xlPivotTableVersion10 refers to Excel 2002
xlPivotTableVersion11 refers to Excel 2003
xlPivotTableVersion12 refers to Excel 2007
xlPivotTableVersion14 refers to Excel 2010
xlPivotTableVersion15 refers to Excel 2013
xlPivotTableVersionCurrent refers to Provided only for backward compatibility
Related
I have a macro that creates a new worksheet and pivot table (which works just fine). As soon as I try to add a second pivot table to this worksheet, I get this error message.
Error message: Run-time error '5': Invalid procedure call or argument
When I create the second pivot in a new worksheet, it works just fine, so I think the issue is when I select add to existing worksheet.
Here is the code for the second pivot (created using a macro, sorry I'm new to this so it may be a bit messy), error is on line 3. Not sure why it references the pivot table "Calls by Region/Analyst", which is the already existing pivot:
Sheets("Sorted Data").Select
ActiveCell.Offset(1, -7).Range("Table1[[#Headers],[Status]]").Select
ActiveWorkbook.Worksheets("Pivot Tables").pivotTables("Calls by Region/Analyst" _
).PivotCache.CreatePivotTable TableDestination:="Pivot Tables!R3C6", _
TableName:="PivotTable6", DefaultVersion:=xlPivotTableVersion12
Sheets("Pivot Tables").Select
Cells(3, 6).Select
ActiveSheet.pivotTables("PivotTable6").Name = "Analyst"
ActiveSheet.pivotTables("Analyst").AddDataField ActiveSheet.pivotTables( _
"Analyst").PivotFields("Assigned User Name"), "Count of Assigned User Name", _
xlCount
ActiveSheet.pivotTables("Analyst").PivotFields("Count of Assigned User Name"). _
Caption = "Number of Calls"
With ActiveSheet.pivotTables("Analyst").PivotFields("Assigned User Name")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.pivotTables("Analyst").CompactLayoutRowHeader = "Analyst"
ActiveCell.Offset(0, 4).Range("A1").Select
End Sub
Here's the code for the second pivot that works when opening it in a new worksheet, however I need it in an existing worksheet (at F3):
Sheets("Sorted Data").Select
ActiveCell.Offset(1, -7).Range("Table1[[#Headers],[Status]]").Select
Sheets.Add
ActiveWorkbook.Worksheets("Pivot Tables").pivotTables("Calls by Region/Analyst" _
).PivotCache.CreatePivotTable TableDestination:="Sheet3!R3C1", TableName:= _
"PivotTable5", DefaultVersion:=xlPivotTableVersion12
Sheets("Sheet3").Select
Cells(3, 1).Select
ActiveSheet.pivotTables("PivotTable5").Name = "Analyst"
ActiveSheet.pivotTables("Analyst").AddDataField ActiveSheet.pivotTables( _
"Analyst").PivotFields("Assigned User Name"), "Count of Assigned User Name", _
xlCount
ActiveSheet.pivotTables("Analyst").PivotFields("Count of Assigned User Name"). _
Caption = "Number of Calls"
With ActiveSheet.pivotTables("Analyst").PivotFields("Assigned User Name")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.pivotTables("Analyst").CompactLayoutRowHeader = "Analyst"
ActiveCell.Offset(1, 0).Range("A1").Select
I'm new at VBA and Macros. Following Macro Recording, I ended up with this bug.
The file name is available on desktop with the correct name.
Thanks for any help,
Sub Macro2435Pivot()
'
' Macro2435Pivot Macro
'
'
Cells.Select
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"2435_Monthly Sales with Adjustm'!1:15000", Version:=xlPivotTableVersion15). _
CreatePivotTable TableDestination:="Sheet2!R3C1", TableName:= _
"PivotTable" & [=round(Rand()*1000,0)], DefaultVersion:=xlPivotTableVersion15
Sheets("Sheet2").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Month")
.Orientation = xlPageField
.Position = 1
End With
Hi I am facing problem in running macro that captured the pivot table creation. I used excel 2016 to create the macros. VBA Pivot Table Run Time Error 5 Invalid Procedure Call for MS office 2016
From this line i get the error: invalid procedure call or argument
ActiveWorkbook.Worksheets("page2").PivotTables("PivotTable1").PivotCache. _ CreatePivotTable TableDestination:="page3!R3C1", TableName:="PivotTable1" _, DefaultVersion:=6
Sub PIVOT()
'
' PIVOT Macro
'
'
Sheets("page1").Select
Sheets.Add
**ActiveWorkbook.Worksheets("page2").PivotTables("PivotTable1").PivotCache. _
CreatePivotTable TableDestination:="page3!R3C1", TableName:="PivotTable1" _
, DefaultVersion:=6**
Sheets("page3").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("fruit")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("fruit"), "Count of fruit", xlCount
With ActiveSheet.PivotTables("PivotTable1").PivotFields("description")
.Orientation = xlPageField
.Position = 1
End With
ActiveWindow.SmallScroll Down:=-3
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Actual")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").PivotFields("description"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("description"). _
CurrentPage = "group"
Range("B15").Select
End Sub
You could try to change your PivotTableCache version, because if you try to run it in an earlier Excel version send this message.
Somethin like in this post:
Runtime Error 5 Sometimes in VBA Creating PivotTable
I created this code with macro recorder to get Pivot table automatically.
But when I run this code again an error message appears:
Run-time error 1004: Invalid reference
at this line Workbooks("works.xlsm").Connections.Add2.
Why is there invalid reference if this code was recorded? During the recording, I gave the name "database" for the table (R1C4:R18532C9). I use Windows 10 and Office 2016.
Range("D1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Names.Add Name:="database", RefersToR1C1:= _
"=Data!R1C4:R18532C9"
ActiveWorkbook.Names("database").Comment = ""
Range("D1").Select
Workbooks("works.xlsm").Connections.Add2 _
"WorksheetConnection_works.xlsm!database", "", _
"WORKSHEET;C:\Users\gabor\Documents\CAFM\VBS\works.xlsm", _
"works.xlsm!database", 7, True, False
ActiveWorkbook.PivotCaches.Create(SourceType:=xlExternal, SourceData:= _
ActiveWorkbook.Connections("WorksheetConnection_works.xlsm!database"), _
Version:=6).CreatePivotTable TableDestination:="Pivot!R1C1", TableName:= _
"Statement1", DefaultVersion:=6
Sheets("Pivot").Select
Cells(1, 1).Select
With ActiveSheet.PivotTables("Statement1").CubeFields("[database].[Person]")
.Orientation = xlRowField
.Position = 1
End With
The database table and the pivot results with xlCount and xlDistinctCount
Try edited code below, explanations are inside the code as comments.
xlDistinctCount is untested as I have Office 2010 (it's available from Officde 2013), but should work.
Code
Option Explicit
Sub AutoDynamicPivot()
Dim PT As PivotTable
Dim PTCache As PivotCache
Dim WB As Workbook
Dim Sht As Worksheet
Dim SrcData As Variant
Dim lRow As Long, lCol As Long
Set WB = ThisWorkbook
Set Sht = WB.Worksheets("Data") '<-- set the "Data" worksheet
lRow = Sht.Range("A1").End(xlDown).Row '<-- modifed from "D1" to "A1" (according to PO screen-shot)
lCol = Sht.Range("A1").End(xlToRight).Column '<-- modifed from "D1" to "A1" (according to PO screen-shot)
' set the Named Range "database" to the data in worksheet "Data"
WB.Names.Add Name:="database", RefersToR1C1:="=" & Sht.Name & "!R1C1:R" & lRow & "C" & lCol '<-- modifed to "R1C1" (according to PO screen-shot)
WB.Names("database").Comment = ""
' Determine the data range you want for your Pivot Cache
Set SrcData = Range("database")
' set the Pivot Cache
Set PTCache = ActiveWorkbook.PivotCaches.Add(xlDatabase, SrcData)
' add this line in case the Pivot table doesn't exit >> first time running this Macro
On Error Resume Next
Set PT = Worksheets("Pivot").PivotTables("Statement1") ' check if "Statement1" Pivot Table already created (in past runs of this Macro)
On Error GoTo 0
If PT Is Nothing Then
' create a new Pivot Table in "Pivot" sheet, start from Cell A1
Set PT = Worksheets("Pivot").PivotTables.Add(PivotCache:=PTCache, TableDestination:=Worksheets("Pivot").Range("A1"), TableName:="Statement1")
'Create the headings and row and column orientation and all of your other settings here
With PT
' set "Person" as rows field
With .PivotFields("Person")
.Orientation = xlRowField
.Position = 1
End With
' set "Month" as Filter
With .PivotFields("Month")
.Orientation = xlPageField
.Position = 1
End With
' set "Count of Cases"
.AddDataField .PivotFields("Case"), "Count of Case", xlCount
' set "Distinct Count of Cases"
.AddDataField .PivotFields("Case"), "Distinct Count of Case", xlDistinctCount
End With
Else
' just refresh the Pivot cache with the updated Range (data in "Data" worksheet)
PT.ChangePivotCache PTCache
PT.RefreshTable
End If
End Sub
Screen-shot of the Pivot-Table created with this code:
I changed the commandtext parameter in Connections.Add2 from "works.xlsm!database" to "Data!database". It solved the problem. I edited the ActiveWorkbook.Names.Add as well.
LastRow = Sheets("Data").Cells(Rows.Count, 1).End(xlUp).Row
LastCol = Sheets("Data").Cells(1, Columns.Count).End(xlToLeft).Column
ActiveWorkbook.Names.Add _
Name:="database", _
RefersTo:="=Data!R1C4:R" & LastRow & "C" & LastCol & ""
ActiveWorkbook.Connections.Add2 _
"WorksheetConnection_works.xlsm!database", "", _
"WORKSHEET;C:\Users\gabor\Documents\CAFM\VBS\works.xlsm", _
"Data!database", 7, True, False
ActiveWorkbook.PivotCaches.Create(SourceType:=xlExternal, SourceData:= _
ActiveWorkbook.Connections("WorksheetConnection_works.xlsm!database"), _
Version:=6).CreatePivotTable TableDestination:="Pivot!R1C1", TableName:= _
"Statement1", DefaultVersion:=6
Sheets("Pivot").Select
Cells(1, 1).Select
With ActiveSheet.PivotTables("Statement1").CubeFields("[database].[Person]")
.Orientation = xlRowField
.Position = 1
End With
I could get the xlDistinctCount with this code:
ActiveSheet.PivotTables("Statement1").CubeFields.GetMeasure "[database].[TT]" _
, xlCount
ActiveSheet.PivotTables("Statement1").AddDataField ActiveSheet.PivotTables( _
"Statement1").CubeFields("[Measures].[quantity of items - TT]")
With ActiveSheet.PivotTables("Statement1").PivotFields( _
"[Measures].[quantity of items - TT]")
.Caption = "number of distinct items – TT"
.Function = xlDistinctCount
End With
I had to use the xlCount first and with this result could I get the xlDistinctCount.
I'm currently having this issue while running VBA code. I've created code that downloads a report from SAP GUI and with that spreadsheet I need to create a PivotTable. I used the Macro Recorder to create the pivot but I'm having this issue while running it.
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Sheet1!R1C1:R1500C18", Version:=xlPivotTableVersion14).CreatePivotTable
TableDestination:="Sheet4!R3C1", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion14
Sheets("Sheet4").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Company Code")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("FI / MM Doc")
.Orientation = xlRowField
.Position = 2
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Document number"), "Count of Document number", _
xlCount
The issue appears in the Tabledestination:="Sheet4!R3C1", row showing up the ":=" as an unexpected error.
Do you guys know how can I solve this?
You're missing the _ symbol to carry over the statement to the next line. That will eliminate your compile error.
However, I'd also look at ensuring your references to Sheet4 are okay: when you add a new worksheet, there's no guarantee that Excel will call it Sheet4, so either you should force the name in the .Add statement, or don't explicitly refer to it as Sheet4 after the creation of the pivot table. When Excel adds a sheet, the new sheet becomes the active one anyway.