【问题标题】:VBA; Invalid procedure or argument errorVBA;无效的过程或参数错误
【发布时间】:2013-07-23 03:56:53
【问题描述】:

一点背景; 我必须在前一周的星期一为我的工作运行每周报告,但是我需要合并材料,我做了并制作了一个数据透视表,我必须为多个工作表执行此操作。但是我决定创建一个宏来完成这个多余的任务。现在创建它我似乎收到此错误消息“无效的过程或参数”。我无法在我的新工作表中打开它,这是我的代码>>

Sub weekmaster()
'
' weekmaster Macro
' Macro for the week
'
' Keyboard Shortcut: Ctrl+t
'
    Cells.Select
    Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "weekmaster!R1C1:R1048576C62", Version:=xlPivotTableVersion12). _
        CreatePivotTable TableDestination:="Sheet9!R3C1", TableName:="PivotTable1" _
        , DefaultVersion:=xlPivotTableVersion12
    Sheets("Sheet9").Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Order ID")
        .Orientation = xlRowField
        .Position = 1
    End With

【问题讨论】:

  • 如果 TableDestination 无效,您可能会收到该错误。 (即不存在具有该名称的工作表。)

标签: vba excel excel-2007 excel-formula


【解决方案1】:

您似乎遗漏了一个论点。 CreatePivotTable 采用以下参数:

表达式.CreatePivotTable(TableDestination, TableName, ReadData, DefaultVersion)

  1. TableDestination 必需变量 数据透视表目标区域(工作表上将放置生成的数据透视表的区域)左上角的单元格。目标范围必须位于工作簿中包含由表达式指定的 PivotCache 对象的工作表上。
  2. TableName 可选 Variant 新数据透视表的名称。
  3. ReadData 可选 Variant True 创建包含来自外部数据库的所有记录的数据透视表缓存;这个缓存可能非常大。 False 以在实际读取数据之前将某些字段设置为基于服务器的页面字段。
  4. DefaultVersion 可选变量 数据透视表的默认版本。

随后,您可能希望在 TableName 和 DefaultVersion 之间添加“true”。

干杯,LC

【讨论】:

    【解决方案2】:

    如果您多次运行该宏,或者Sheet9 已经存在(因为该宏尝试在同一张工作表上创建同一个名称相同的数据透视表),您将收到错误消息。如果我假设您每次转到数据表并运行宏时都会在 new 工作表中生成一个 new 数据透视表,那么您可以使用以下:

    Dim myRange as Range
    dim myNewSheet as Worksheet
    
    ' Stores all continuous data on the sheet in the myRange variable
    Set myRange = Range("A1").CurrentRegion
    
    ' Adds a new sheet and stores it in the myNewSheet variable
    Set myNewSheet = Sheets.Add
    
    ' Use the variables to create the new pivot table
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        myRange, Version:=xlPivotTableVersion12).CreatePivotTable _
        TableDestination:=myNewSheet.Cells(3, 1), DefaultVersion _
        :=xlPivotTableVersion12
    
    ' Select your Order ID field
    With myNewSheet.PivotTables(1).PivotFields("Order ID")
        .Orientation = xlRowField
        .Position = 1
    End With
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-11
      • 1970-01-01
      • 2020-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多