【问题标题】:Pivot Table Formatting returns error数据透视表格式返回错误
【发布时间】:2012-12-05 13:15:28
【问题描述】:

我正在开发一个宏,其中我将数据透视表放在一起。

我的问题: 我无法让计算部分工作。每次我在 PivotField(18) 应该计算 .Function=xlCount 的阶段运行代码时都会出错。我得到的错误是“范围类的 1004 选择方法失败”。

我在网上和 MS 帮助部分找到的所有示例都是按照我设置代码的方式构建的。我错过了什么???

任何帮助都非常感谢......

我一天中大部分时间都在处理这个问题。以下页面 (Link) 非常有帮助,但并没有帮助我解决这个特殊问题。

' Start of PivotSpecific Formatting
Set pt = ActiveSheet.PivotTables("PT" & wsNewWorksheetName)
With pt
    ' Prevent AutoFormatting of Columns
    .HasAutoFormat = False
    ' Hide Field Headers
    .DisplayFieldCaptions = False
    .TableStyle2 = "PivotStyleMedium2"
    ' Apply calculation to numbers and change Caption
    With .PivotFields(18)
        .Caption = "Demand"
        .Function = xlCount
        .NumberFormat = "#,##0"
    End With
    With .PivotFields(15)
        .Caption = "Lowest OP"
        .Function = xlMin
        .NumberFormat = "#,##0.00"
    End With
End With

更新 1:数据透视表的屏幕截图

更新 2:来自 bonCodigo 的测试代码 这是针对我的工作簿调整的代码。有了这个,我得到了一个运行时错误 91: 设置 pvField = pvTable.PivotFields(18)

Dim wkSheet As Worksheet
Dim pvTable As PivotTable
Dim pvField As PivotField

Set wkSheet = ThisWorkbook.Worksheets(wsNewWorksheetName)
Set pvTble = wkSheet.PivotTables("PT" & wsNewWorksheetName)
Set pvField = pvTable.PivotFields(18)

With pvField
     .Caption = "Demand"
     .Function = xlCount
     .orientation = xlDataField
     .NumberFormat = "#,##0"
End With

Set pvField = Nothing
Set pvTable = Nothing
Set wkSheet = Nothing

【问题讨论】:

  • 你能告诉我们你的数据透视表是什么样子的吗?这段代码你期望得到什么结果?你只是在.Function = xlCount 中得到错误吗? .function = xlMin 怎么样?你的 .PivotFields(18) 叫什么名字?你确定是count?还是像sum这样的其他东西?
  • @bonCodigo 我在代码中的 .Function= 上都得到了它。我将上传表格的屏幕截图。
  • @bonCodigo PivotFields(18) 的名称是“Nachfrage”...但是,我不想使用该名称,因为该应用程序将在各种计算机上使用,并非所有计算机都将具有相同的语言设置...这意味着“Anzahl von”(英文为“Count of”)在运行宏时可能会导致问题。

标签: excel excel-2010 pivot-table vba


【解决方案1】:

请试试这个并评论你的结果。所以我们可以从那里看到。

Option Explicit

'-- whatever other code you may have... 

Dim wkSheet as Worksheet
Dim pvTable as PivotTable
Dim pvField as PivotField

Set wkSheet = ThisWorkbook.Worksheets("Sheet1") '-- user proper sheet name explicitly
Set pvTble = wkSheet.PivotTables("PivotTable1") 
   ' or wkSheet.PivotTables(1) '-- use pivot table name or index
Set pvField = pvTable.PivotFields("SomeField") '-- use pivot field name or index

' -- do whatever you want to do with the pivot table

' -- do what you need to do with the pivot field
With pvField
     .Caption = "Demand"
     .Orientation = xlDataField '--<< THIS could well be the culprit
     .Function = xlCount
     .NumberFormat = "#,##0"
End With

Set pvField = Nothing
Set PvTable = Nothing
Set wkSheet = Nothing

由于您要格式化多个字段,如果它们是连续的,我建议您使用循环使用DataFields Index

例如pvTable.DataFields(1).Function = xlCount

否则您将不得不单独定义它们。最好将变量分配为对要交互的对象的引用。 :)

【讨论】:

  • 谢谢。请参阅上面的更新 2!
  • 已更新orientation,请测试一下。
  • 我插入了.orientation,但它实际上在它到达之前就停止了......我在Set pvField = pvTable.PivotFields(18)上遇到运行时错误
  • 为什么要SELECT?只需删除那些破坏性能的选择:(当您设置时,您已经在跟踪这些对象......请使用我给您的代码,不要使用其他任何东西。建议不要使用任何变量名称作为名称。只需使用纯系统名称,例如 Sheet1 blah blah...我认为您的名称变量之一有问题。
  • 我松了一口气,很高兴你解决了这个问题。至于最好的猜测,设置错误名称的对象会导致灾难。 :-) 所以当你想访问那个对象时,没有这样的东西存在。所以炸弹。为你的毅力+1。
猜你喜欢
  • 2016-09-24
  • 1970-01-01
  • 1970-01-01
  • 2012-10-09
  • 1970-01-01
  • 1970-01-01
  • 2013-11-15
  • 1970-01-01
相关资源
最近更新 更多