.datapivotfield.orientation = xlhidden 不起作用
.datapivotfield.orientation = xlhidden does not work
我有一个 vba 代码,它检查指定工作表中的所有数据透视表以将一系列数据透视表字段添加到数据透视表的 values/data 部分,因为数据透视表 headers 每 4 次更改一次周。
我有 6 个关键点,此代码可以完美运行,但有 2 个关键点我不断出错。我试图调整代码但没有成功。我使用这段代码找到了这个数据透视字段的类型,因为我认为它应该是数据,但结果是 'hidden':
Set pvtTable = Worksheets("Sheet1").Range("A3").PivotTable
Set pvtField = pvtTable.PivotFields("ORDER_DATE")
Select Case pvtField.Orientation
Case xlHidden
MsgBox "Hidden field"
Case xlRowField
MsgBox "Row field"
Case xlColumnField
MsgBox "Column field"
Case xlPageField
MsgBox "Page field"
Case xlDataField
MsgBox "Data field"
End Select
然后我检查了这段代码在它运行良好的枢轴中的可比较字段上,期望它是数据字段,但它又是隐藏字段。因此,我不明白与代码完美运行的枢轴相比,这两个枢轴有什么不同使我的代码无法正常工作。
这是它不起作用的枢轴代码:
Sub AddAllFieldsValues_blad1()
Dim pt As PivotTable
Dim iCol As Long
Dim iColEnd As Long
Dim sheetnames As Variant
Dim I As Variant
With Sheets("blad1")
For Each pt In Sheets("blad1").PivotTables
With pt
.ManualUpdate = True
.DataPivotField.Orientation = xlHidden
iCol = 11
With .PivotFields(iCol)
If .Orientation = 0 Then
.Orientation = xlDataField
End If
End With
.ManualUpdate = False
pt.PivotCache.refresh
End With
Next pt
End With
End Sub
我只需要数据中心在值部分有 1 个数据中心字段。
错误 1004:
属性 无法设置 class 数据透视字段的方向
当我点击 'solve error' 时,这一行被标记(或者它的英文名称):
.DataPivotField.Orientation = xlHidden
我不明白为什么,因为它非常适合工作表中的其他数据透视表。唯一不同的是,对于那些枢轴,代码略有不同:
Sub AddAllFieldsValues()
Dim pt As pivottable
Dim iCol As Long
Dim iColEnd As Long
Dim sheetnames As Variant
Dim I As Variant
sheetnames = Array("data pivots euros", "data pivots category - euros", "data pivots units", "data pivots category - units")
For I = LBound(sheetnames) To UBound(sheetnames)
With Sheets(sheetnames(I))
For Each pt In Sheets(sheetnames(I)).PivotTables
With pt
.ManualUpdate = True
.DataPivotField.Orientation = xlHidden
iCol = 12
iColEnd = .PivotFields.Count - 4
For iCol = 12 To iColEnd
With .PivotFields(iCol)
If .Orientation = 0 Then
.Orientation = xlDataField
End If
End With
Next iCol
.ManualUpdate = False
pt.PivotCache.refresh
End With
Next pt
End With
Next I
End Sub
DataPivotField 属性 有问题,因为它只能工作,
当您的数据透视表已经 至少 2 个数据字段时 !
如果您想先删除每个数据字段,然后替换行
.DataPivotField.Orientation = xlHidden
通过这个:
Dim df as PivotField
For Each df In .DataFields
df.Orientation = xlHidden
Next df
很晚才回答,但如果有人看了,我相信这复制了问题的内容。
Dim xPivot As PivotTable
'setting exact pivot table also ensuring its targeting the correct sheet
Set xPivot = ThisWorkbook.Worksheets("Sheet1").PivotTables("Pivot_Table_Name")
'change "values" as required to view data fields/row fields etc
'change "Field Name" to match the value name
xPivot.PivotFields("Values").PivotItems("FieldName").Visible = False
我有一个 vba 代码,它检查指定工作表中的所有数据透视表以将一系列数据透视表字段添加到数据透视表的 values/data 部分,因为数据透视表 headers 每 4 次更改一次周。
我有 6 个关键点,此代码可以完美运行,但有 2 个关键点我不断出错。我试图调整代码但没有成功。我使用这段代码找到了这个数据透视字段的类型,因为我认为它应该是数据,但结果是 'hidden':
Set pvtTable = Worksheets("Sheet1").Range("A3").PivotTable
Set pvtField = pvtTable.PivotFields("ORDER_DATE")
Select Case pvtField.Orientation
Case xlHidden
MsgBox "Hidden field"
Case xlRowField
MsgBox "Row field"
Case xlColumnField
MsgBox "Column field"
Case xlPageField
MsgBox "Page field"
Case xlDataField
MsgBox "Data field"
End Select
然后我检查了这段代码在它运行良好的枢轴中的可比较字段上,期望它是数据字段,但它又是隐藏字段。因此,我不明白与代码完美运行的枢轴相比,这两个枢轴有什么不同使我的代码无法正常工作。
这是它不起作用的枢轴代码:
Sub AddAllFieldsValues_blad1()
Dim pt As PivotTable
Dim iCol As Long
Dim iColEnd As Long
Dim sheetnames As Variant
Dim I As Variant
With Sheets("blad1")
For Each pt In Sheets("blad1").PivotTables
With pt
.ManualUpdate = True
.DataPivotField.Orientation = xlHidden
iCol = 11
With .PivotFields(iCol)
If .Orientation = 0 Then
.Orientation = xlDataField
End If
End With
.ManualUpdate = False
pt.PivotCache.refresh
End With
Next pt
End With
End Sub
我只需要数据中心在值部分有 1 个数据中心字段。
错误 1004: 属性 无法设置 class 数据透视字段的方向
当我点击 'solve error' 时,这一行被标记(或者它的英文名称):
.DataPivotField.Orientation = xlHidden
我不明白为什么,因为它非常适合工作表中的其他数据透视表。唯一不同的是,对于那些枢轴,代码略有不同:
Sub AddAllFieldsValues()
Dim pt As pivottable
Dim iCol As Long
Dim iColEnd As Long
Dim sheetnames As Variant
Dim I As Variant
sheetnames = Array("data pivots euros", "data pivots category - euros", "data pivots units", "data pivots category - units")
For I = LBound(sheetnames) To UBound(sheetnames)
With Sheets(sheetnames(I))
For Each pt In Sheets(sheetnames(I)).PivotTables
With pt
.ManualUpdate = True
.DataPivotField.Orientation = xlHidden
iCol = 12
iColEnd = .PivotFields.Count - 4
For iCol = 12 To iColEnd
With .PivotFields(iCol)
If .Orientation = 0 Then
.Orientation = xlDataField
End If
End With
Next iCol
.ManualUpdate = False
pt.PivotCache.refresh
End With
Next pt
End With
Next I
End Sub
DataPivotField 属性 有问题,因为它只能工作,
当您的数据透视表已经 至少 2 个数据字段时 !
如果您想先删除每个数据字段,然后替换行
.DataPivotField.Orientation = xlHidden
通过这个:
Dim df as PivotField
For Each df In .DataFields
df.Orientation = xlHidden
Next df
很晚才回答,但如果有人看了,我相信这复制了问题的内容。
Dim xPivot As PivotTable 'setting exact pivot table also ensuring its targeting the correct sheet Set xPivot = ThisWorkbook.Worksheets("Sheet1").PivotTables("Pivot_Table_Name") 'change "values" as required to view data fields/row fields etc 'change "Field Name" to match the value name xPivot.PivotFields("Values").PivotItems("FieldName").Visible = False