我正在研究一个将数据透视表放在一起的宏。
我的问题:我无法让计算部分工作。每次我在 PivotField(18) 应该计算 .Function=xlCount 的阶段运行代码时都会出错。我得到的错误是“范围类的 1004 选择方法失败”。
我在网上和 MS 帮助部分找到的所有示例都是按照我设置代码的方式构建的。我错过了什么???
任何帮助都非常感谢...
我一天中大部分时间都在努力解决这个问题。以下页面(链接)非常有帮助,但并没有帮助我解决这个特殊问题。
' 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: Set 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