Excel vba:在透视表中隐藏计算字段时出错
我已经编写了几个sub来显示/隐藏数据透视表中的字段。 现在,我试图对计算字段执行相同的操作,但在隐藏它时会出错。 我从录音机中取出代码,录音机的代码也停在最后一行。 我在谷歌上搜索了错误消息,没有得到严重的结果Excel vba:在透视表中隐藏计算字段时出错,vba,excel,pivot-table,Vba,Excel,Pivot Table,我已经编写了几个sub来显示/隐藏数据透视表中的字段。 现在,我试图对计算字段执行相同的操作,但在隐藏它时会出错。 我从录音机中取出代码,录音机的代码也停在最后一行。 我在谷歌上搜索了错误消息,没有得到严重的结果 Sub PrRemove() 'remove PR Dim pt As PivotTable Set pt = ActiveSheet.PivotTables("MyPivot") pt.PivotFields("MyField").Orientatio
Sub PrRemove()
'remove PR
Dim pt As PivotTable
Set pt = ActiveSheet.PivotTables("MyPivot")
pt.PivotFields("MyField").Orientation = xlHidden '<- here is the error
End Sub
Sub-PrRemove()
'删除PR
数据透视表
Set pt=ActiveSheet.PivotTables(“MyPivot”)
pt.PivotFields(“MyField”).Orientation=xlHidden'好吧,我会给你所需要的确认。在“计算字段”上使用Orientation
属性似乎不起作用,我必须承认这是一个错误,而不是常见的“用法”错误。我能够复制“隐藏/显示”字段,而不必删除(“删除”)计算字段。这允许用户在编程“隐藏”字段后,从字段列表中实际拖动计算字段。这是一个不错的解决方案,因为它复制了用户界面。(使用Excel 2003。)
[原答覆]
您很可能无法隐藏此项目,因为它是最后一个可见的项目。相反,请尝试删除它。好吧,我会给您所需的确认。在“计算字段”上使用Orientation
属性似乎不起作用,我必须承认这是一个错误,而不是常见的“用法”错误。我能够复制“隐藏/显示”字段,而不必删除(“删除”)计算字段。这允许用户在编程“隐藏”字段后,从字段列表中实际拖动计算字段。这是一个不错的解决方案,因为它复制了用户界面。(使用Excel 2003。)
[原答覆]
您很可能无法隐藏此项目,因为它是最后一个可见的项目。请尝试将其删除。是否更改了计算字段的名称?它最初是“我的领域的总和”吗?请尝试查看SourceName属性,如果它与此不同,请使用它
您是否尝试过计算字段(“MyField”).Orientation=xlHidden
?您是否更改了计算字段的名称?它最初是“我的领域的总和”吗?请尝试查看SourceName属性,如果它与此不同,请使用它
您是否尝试过
pt.CalculatedFields(“MyField”).Orientation=xlHidden
?我不认为这是一个excel错误,我认为这是一个“功能”。;-)
Re:@AMissico,excel隐藏数据透视表中的所有字段没有问题,但他可能在谈论项目-你不能隐藏数据透视字段中的最后一个项目 这是我经常用来做你想做的事情的代码。这些宏是在Excel2002和2003上开发的。我不隐藏计算字段,而是删除它们
' Hide all fields.
' @param ThePivotTable to operate upon.
Sub HidePivotFields(ByVal ThePivotTable As PivotTable)
Dim pField As PivotField
For Each pField In ThePivotTable.CalculatedFields
pField.Delete
Next pField
For Each pField In ThePivotTable.PivotFields
pField.Orientation = xlHidden
Next pField
Set pField = Nothing
End Sub
' Removes FieldName data from ThePivotTable
Sub HideField(ByVal ThePivotTable As PivotTable, _
ByVal FieldName As String)
If FieldExists(ThePivotTable, FieldName) = True And _
CalculatedFieldExists(ThePivotTable, FieldName) = False Then
ThePivotTable.PivotFields(FieldName).Orientation = xlHidden
End If
End Sub
' Returns True if FieldName exists in ThePivotTable
'
' @param ThePivotTable to operate upon.
' @param FieldName the name of the specific pivot field.
Function FieldExists(ByVal ThePivotTable As PivotTable, _
ByVal FieldName As String) As Boolean
Dim pField As PivotField
For Each pField In ThePivotTable.PivotFields
If pField.SourceName = FieldName Then
FieldExists = True
Exit For
End If
Next pField
Set pField = Nothing
End Function
' Checks if the field FieldName is currently a member of the
' CalculatedFields Collection in ThePivotTable.
' @return True if a CalculatedField has a SourceName matching the FieldName
' @return False otherwise
Function CalculatedFieldExists(ByVal ThePivotTable As PivotTable, _
ByVal FieldName As String) As Boolean
Dim pField As PivotField
CalculatedFieldExists = False
For Each pField In ThePivotTable.CalculatedFields
If pField.SourceName = FieldName Then
CalculatedFieldExists = True
End If
Next pField
Set pField = Nothing
End Function
' Returns a Pivot Field reference by searching through the source names.
'
' This function is a guard against the user having changed a field name on me.
' @param ThePivotTable to operate upon.
' @param FieldName the name of the specific pivot field.
Function GetField(ByVal ThePivotTable As PivotTable, _
ByVal FieldName As String) As PivotField
Dim pField As PivotField
For Each pField In ThePivotTable.PivotFields
If pField.Name <> "Data" Then
If pField.SourceName = FieldName Then
Set GetField = pField
Exit For
End If
End If
Next pField
Set pField = Nothing
End Function
' Counts the number of currently visible pivot items in a field.
' @param ThePivotItems the collection of pivot itemns in a field.
' @return the count of the visible items.
Function PivotItemCount(ByVal ThePivotItems As PivotItems) As Long
Dim pItem As PivotItem
Dim c As Long
For Each pItem In ThePivotItems
If pItem.Visible = True Then c = c + 1
Next pItem
PivotItemCount = c
Set pItem = Nothing
End Function
' Hides a single pivot item in a pivot field, unless it's the last one.
' @param FieldName pivot field containing the pivot item.
' @param ItemName pivot item to hide.
Sub HidePivotItem(ByVal ThePivotTable As PivotTable, _
ByVal FieldName As String, _
ByVal ItemName As String)
Dim pField As PivotField
Set pField = GetField(ThePivotTable, FieldName)
If Not pField Is Nothing Then
If PivotItemCount(pField.PivotItems) > 1 Then
pField.PivotItems(ItemName).Visible = False
End If
End If
Set pField = Nothing
End Sub
”隐藏所有字段。
“@param可对其进行操作。
子HidePivotFields(通过VAL将PivotTable设置为数据透视表)
作为数据透视字段的Dim pField
对于Pivottable.Calculated字段中的每个P字段
删除
下一个普菲尔德
对于pivottable.pivottable字段中的每个pField
pField.Orientation=xlHidden
下一个普菲尔德
设置pField=Nothing
端接头
'从个人资料表中删除字段名数据
子HideField(通过将PivotTable作为数据透视表_
ByVal字段名(以字符串形式)
如果字段存在(该选项,字段名)=True和_
CalculatedFieldExists(pVipottable,FieldName)=False然后
Pivottable.PivotFields(FieldName).Orientation=xlHidden
如果结束
端接头
'如果字段名存在于个人资料表中,则返回True
'
“@param可对其进行操作。
“@param FieldName特定透视字段的名称。
函数字段exists(通过VAL将PivotTable设置为数据透视表_
ByVal FieldName(作为字符串)作为布尔值
作为数据透视字段的Dim pField
对于pivottable.pivottable字段中的每个pField
如果pField.SourceName=FieldName,则
FieldExists=True
退出
如果结束
下一个普菲尔德
设置pField=Nothing
端函数
'检查字段名当前是否为
'个人资料表中的CalculatedFields集合。
“@如果CalculatedField的SourceName与FieldName匹配,则返回True
“@否则返回False
函数CalculatedFieldExists(通过VAL将该变量设置为数据透视表_
ByVal FieldName(作为字符串)作为布尔值
作为数据透视字段的Dim pField
CalculatedFieldExists=False
对于Pivottable.Calculated字段中的每个P字段
如果pField.SourceName=FieldName,则
CalculatedFieldExists=True
如果结束
下一个普菲尔德
设置pField=Nothing
端函数
'通过搜索源名称返回数据透视字段引用。
'
'此函数用于防止用户更改我的字段名。
“@param可对其进行操作。
“@param FieldName特定透视字段的名称。
函数GetField(ByVal)作为数据透视表_
ByVal FieldName(作为字符串)作为数据透视字段
作为数据透视字段的Dim pField
对于pivottable.pivottable字段中的每个pField
如果是pField.Name“Data”,则
如果pField.SourceName=FieldName,则
设置GetField=pField
退出
如果结束
如果结束
下一个普菲尔德
设置pField=Nothing
端函数
'统计字段中当前可见的轴项目数。
“@param ThePivotItems字段中的透视项集合。
“@返回可见项目的计数。
函数PivotItemCount(将PivotItems作为PivotItems进行VAL)的长度为
作为数据透视项的Dim pItem
尺寸c与长度相同
对于项目中的每个项目
如果pItem.Visible=True,则c=c+1
下一个坑
PivotItemCount=c
设置pItem=Nothing
端函数
'隐藏透视字段中的单个透视项,除非它是最后一个透视项。
“@param FieldName包含透视项的透视字段。
“@param ItemName要隐藏的透视项。
亚隐孢子虫(B
' Hide all fields.
' @param ThePivotTable to operate upon.
Sub HidePivotFields(ByVal ThePivotTable As PivotTable)
Dim pField As PivotField
For Each pField In ThePivotTable.CalculatedFields
pField.Delete
Next pField
For Each pField In ThePivotTable.PivotFields
pField.Orientation = xlHidden
Next pField
Set pField = Nothing
End Sub
' Removes FieldName data from ThePivotTable
Sub HideField(ByVal ThePivotTable As PivotTable, _
ByVal FieldName As String)
If FieldExists(ThePivotTable, FieldName) = True And _
CalculatedFieldExists(ThePivotTable, FieldName) = False Then
ThePivotTable.PivotFields(FieldName).Orientation = xlHidden
End If
End Sub
' Returns True if FieldName exists in ThePivotTable
'
' @param ThePivotTable to operate upon.
' @param FieldName the name of the specific pivot field.
Function FieldExists(ByVal ThePivotTable As PivotTable, _
ByVal FieldName As String) As Boolean
Dim pField As PivotField
For Each pField In ThePivotTable.PivotFields
If pField.SourceName = FieldName Then
FieldExists = True
Exit For
End If
Next pField
Set pField = Nothing
End Function
' Checks if the field FieldName is currently a member of the
' CalculatedFields Collection in ThePivotTable.
' @return True if a CalculatedField has a SourceName matching the FieldName
' @return False otherwise
Function CalculatedFieldExists(ByVal ThePivotTable As PivotTable, _
ByVal FieldName As String) As Boolean
Dim pField As PivotField
CalculatedFieldExists = False
For Each pField In ThePivotTable.CalculatedFields
If pField.SourceName = FieldName Then
CalculatedFieldExists = True
End If
Next pField
Set pField = Nothing
End Function
' Returns a Pivot Field reference by searching through the source names.
'
' This function is a guard against the user having changed a field name on me.
' @param ThePivotTable to operate upon.
' @param FieldName the name of the specific pivot field.
Function GetField(ByVal ThePivotTable As PivotTable, _
ByVal FieldName As String) As PivotField
Dim pField As PivotField
For Each pField In ThePivotTable.PivotFields
If pField.Name <> "Data" Then
If pField.SourceName = FieldName Then
Set GetField = pField
Exit For
End If
End If
Next pField
Set pField = Nothing
End Function
' Counts the number of currently visible pivot items in a field.
' @param ThePivotItems the collection of pivot itemns in a field.
' @return the count of the visible items.
Function PivotItemCount(ByVal ThePivotItems As PivotItems) As Long
Dim pItem As PivotItem
Dim c As Long
For Each pItem In ThePivotItems
If pItem.Visible = True Then c = c + 1
Next pItem
PivotItemCount = c
Set pItem = Nothing
End Function
' Hides a single pivot item in a pivot field, unless it's the last one.
' @param FieldName pivot field containing the pivot item.
' @param ItemName pivot item to hide.
Sub HidePivotItem(ByVal ThePivotTable As PivotTable, _
ByVal FieldName As String, _
ByVal ItemName As String)
Dim pField As PivotField
Set pField = GetField(ThePivotTable, FieldName)
If Not pField Is Nothing Then
If PivotItemCount(pField.PivotItems) > 1 Then
pField.PivotItems(ItemName).Visible = False
End If
End If
Set pField = Nothing
End Sub
'Add Sales data field
ActiveSheet.PivotTables(Pname).AddDataField ActiveSheet.PivotTables( _
Pname).PivotFields("SALES"), "Sum of SALES", xlSum
'At this point, the datafield titles are in vertically adjacent rows, named "Sum
'of Margin" and "Sum of Sales" at locations B3 and B4 respectively.
'Remove the "Sum of Margin" calculated field
Range("B3").Delete
Sub Macro1()
Dim p As PivotTable
Dim f As PivotField
Set p = ActiveSheet.PivotTables(1)
For Each f In p.PivotFields
If f.Orientation <> xlHidden Then
f.Orientation = xlHidden
End If
Next f
For Each f In p.DataFields
If f.Orientation <> xlHidden Then
f.Orientation = xlHidden
End If
Next f
End Sub
objTable.DataPivotField.Orientation = xlHidden
PivotTable(1).PivotFields("Values").Orientation = xlHidden
For Each PF In PT.DataFields
PF.Orientation = xlHidden
Next PF
With ActiveSheet.PivotTables("PivottableName").PivotFields("Values")
.PivotItems("CalcFieldName").Visible = False
End With
Set pt = ActiveSheet.PivotTables("mypivottable")
For Each pi In pt.DataPivotField.PivotItems
pi.Visible = False
Next
Private Sub Refresh_Pivot()
Dim NewMetric As String
Dim pt As PivotTable, objDataField As Object
NewMetric = "your_custom_metric"
'-------update pivot table 1, hide all elements including calculated field----
Application.EnableEvents = False
Set pt = Sheet1.PivotTables("PivotTable1")
For Each Pi In pt.DataPivotField.PivotItems
Pi.Visible = False
Next
'--------add a new data field to the pivot table----------------------------
With pt
.AddDataField.PivotFields(NewMetric), "Sum of " & NewMetric, xlSum
End With
Application.EnableEvents = True
End Sub
Sub PrRemove()
'remove PR
Dim pt As PivotTable
Set pt = ActiveSheet.PivotTables("MyPivot")
pt.CubeFields("MyField").Orientation = xlHidden '<- here is the error
End Sub
Public Sub PivotFieldsChange()
Dim ValType As String, param As String
Dim pf As PivotField
Dim pt As PivotTable
Application.ScreenUpdating = False
Sheet = "mysheet"
'select between % calculated column or normal column
If Range("Z1").Value = 1 Then
ValType = "%"
Else: ValType = ""
End If
Application.EnableEvents = False
For Each pt In Sheets(Sheet).PivotTables
Select Case pt.Name
Case "case1": param = "param1"
Case "case2": param = "param2"
Case "case3": param = "param3"
Case Else: GoTo line1
End Select
pt.PivotFields("Values").PivotItems("X").Visible = False
pt.PivotFields("Values").PivotItems("Y").Visible = False
pt.PivotFields (param & ValType & "_X")
pt.PivotFields(param & ValType & "_X").Orientation = xlDataField
pt.PivotFields (param & ValType & "_Y")
pt.PivotFields(param & ValType & "_Y").Orientation = xlDataField
For Each pf In pt.DataFields
pf.Function = xlAverage
pf.Caption = Right(pf.Caption, 1)
Next
line1:
Next pt
Application.EnableEvents = True
End Sub