I am using the code below to try and create a pivot table in Excel, but it doesn't appear to be working
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Dim AllData As Range
Set AllData = selection.CurrentRegion
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=AllData).CreatePivotTable TableDestination:= _
"'[Process Data v2.xls]Pivot'!R4C1", TableName:="PivotTable3", _
DefaultVersion:=xlPivotTableVersion10
The error is with the pivot table section, and its a runtime error 5. Invalid procedure call or argument.
I have tried recording a macro and having a precise range in the script and it works, but replacing this with the 'Alldata' variable breaks it. I tried a lot of things but can't figure out why its not working
Try splitting up your code so that the pivot cache and table creation are separated: thet will give you a better idea of where the actual error lies:
Dim pc, pt
Dim AllData As Range
Set AllData = Range("A1").CurrentRegion
Set pc = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:=AllData)
Set pt = pc.CreatePivotTable TableDestination:= _
"'[Process Data v2.xls]Pivot'!R4C1", _
TableName:="PivotTable3", DefaultVersion:=xlPivotTableVersion10
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.