简体   繁体   中英

Issue with vba creating a pivot table, is this an excel version issue?

Quick one, got the following code which goes and creates a pivot table based on a data collection "Data". I run this on the PC that it was created on which runs Excel 2013 without an issue however when I run it on Excel 2007 or Excel 2010 it throws up an Invalid Procedure Call or Arguement error on the line after Range("A1").Select

Is this an excel version error because written in Excel 2013 but used on 2007 or 2010?

    Sheets("Pivot").Select
    Range("A1").Select
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Data", Version:=xlPivotTableVersion15).CreatePivotTable TableDestination:= _
        "Pivot!R1C1", TableName:="PivotTable4", DefaultVersion:= _
        xlPivotTableVersion15
    Sheets("Pivot").Select
    Cells(1, 1).Select
    With ActiveSheet.PivotTables("PivotTable4").PivotFields("Date Only")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable4").PivotFields("Species")
        .Orientation = xlColumnField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable4").AddDataField ActiveSheet.PivotTables( _
        "PivotTable4").PivotFields("Number"), "Sum of Number", xlSum

If it is a version issue which I think it might be as people have used DefaultVersion:=xlPivotTableVersion14 and 12 does anyone know which version would work on Excel 2007 or Excel 2010? I am assuming if I input correct version for 2007 it would be backwards compatible with 2010 and 2013 etc...?

Cheers in advance Will

I think it's an issue with Version:=xlPivotTableVersion15 , which is specific to Excel 2013. Here's the help from Excel 2010:

在此处输入图片说明

It is an optional argument and, in Excel 2010, the pivot table was created correctly with and without it.

I imagine it would be backwards compatible but you could always check the version and build your code accordingly. I believe

application.version

will get you the current version in excel.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM