1

ピボットテーブルのフィールドを表示/非表示にするために、いくつかのSubを作成しました。今、計算フィールドで同じことをしようとしていますが、非表示にするとエラーが発生します。レコーダーからコードを取得しましたが、レコーダーのコードも最後の行で停止します。深刻な結果なしに、エラーメッセージをグーグルで検索しました。

Sub PrRemove()
    'remove PR
    Dim pt As PivotTable
    Set pt = ActiveSheet.PivotTables("MyPivot")
    pt.PivotFields("MyField").Orientation = xlHidden   '<- here is the error
End Sub

MyFieldが通常のフィールド(計算されたフィールドではない)の場合、同じコードが正常に機能します。
SP2でExcel2007を使用しています。
どんな手掛かり ?

2010年6月17日の編集:pt.PivotFieldsの代わりにpt.DataFieldsを使用してみましたが、まったく同じ動作でした。エラーメッセージには、「PivotFieldクラスの方向を設定できません」と表示されます。

4

14 に答える 14

5

多くの髪を引っ張った後、私は回避策を見つけました。複数のピボットフィールド(計算またはその他)を追加すると、ExcelはValuesと呼ばれるグループ化されたフィールドを作成します。PivotField( "Values")のorientationプロパティをxlHiddenに設定すると、両方のフィールドに箇条書きが表示されます。したがって、計算フィールドを削除する場合は、計算されていないフィールドを追加し、Pivo​​tField( "Values")。orientationをxlHiddenに設定するだけで、完了です。

誰もそれがきれいになるとは言いませんでした...

于 2010-07-22T16:12:49.420 に答える
5
With ActiveSheet.PivotTables("PivottableName").PivotFields("Values")
    .PivotItems("CalcFieldName").Visible = False
End With
于 2015-03-24T20:06:28.327 に答える
3

手動で行うように、データフィールド(計算フィールドかどうか)を簡単に削除したかったのです。

そして私はついにこの解決策を見つけました(Excel 2010):

Set pt = ActiveSheet.PivotTables("mypivottable")
For Each pi In pt.DataPivotField.PivotItems
    pi.Visible = False
Next
于 2016-01-13T15:27:27.713 に答える
1

さて、私はあなたが必要とする確認をあなたに与えるでしょう。Orientation「CalulatedField」でプロパティを使用することは機能しないようです。これはバグであり、一般的な「使用法」エラーではないことに同意する必要があります。計算フィールドを削除(「削除」)することなく、フィールドの「非表示/表示」を複製することができました。これにより、フィールドをプログラムで「非表示」にした後、ユーザーはフィールドリストから計算フィールドを物理的にドラッグできます。これはユーザーインターフェイスを複製するため、悪い解決策ではありません。(Excel 2003を使用します。)

'2009.09.25 AMJ
'work around for
'   1004, Unable to set the Orientation property of the PivotField class
'when setting orientation property to hidden of calculated field, as in
'   ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of Field1").Orientation = xlHidden

Public Sub Hide()
'hide the data without removing the calculated field
'   this allows the user to physically drag the
'       calculated field from the field list once we
'       have "hidden" it programmatically.
'   if we use the "delete" method, the field is removed
'       from the pivot table and the field list

    Dim oWS As Worksheet
    Dim oPT As PivotTable
    Dim oPF As PivotField
    Dim oPI As PivotItem

    Set oWS = ActiveSheet
    Set oPT = oWS.PivotTables(1)

    For Each oPF In oPT.DataFields
        If oPF.SourceName = "Field1" Then
            'Stop
            Exit For
        End If
    Next

    Set oPI = oPF.DataRange.Cells(1, 1).PivotItem
    'oPI.DataRange.Select
    oPI.Visible = False

End Sub

Public Sub Show()
'show just reads the pivot field to the data fields

    Dim oWS As Worksheet
    Dim oPT As PivotTable
    Dim oPF As PivotField

    Set oWS = ActiveSheet
    Set oPT = oWS.PivotTables(1)

    For Each oPF In oPT.PivotFields
        If oPF.SourceName = "Field1" Then
            'Stop
            Exit For
        End If
    Next

    oPT.AddDataField oPF

End Sub

[元の回答] このアイテムは最後に表示されるアイテムであるため、非表示にできない可能性があります。代わりに、削除してみてください。

于 2009-09-07T09:10:16.300 に答える
1

これが今日私が発見した小さな回避策です。これもあまりエレガントではありませんが、少なくとも多くのコードは必要ありません。すべてのフィールドが非表示になり、必要なフィールドを再表示する必要があります。

objTable.DataPivotField.Orientation = xlHidden

何らかの理由でExcelがデータピボットフィールドが空であると判断した場合、エラーが発生する可能性がありますが、これを修正するには、上記のステートメントの直前にデータフィールドとして別のフィールドを追加するだけです。また、xlHidden vbaのデフォルトフォントの数字1ではなく文字lが、非常によく似ていることを確認してください。

ハッピーコーディング

于 2013-08-28T15:47:35.303 に答える
1

計算フィールドを非表示にするには、最初に「値」と呼ばれるピボットフィールドを非表示にする必要があるようです。

PivotTable(1).PivotFields("Values").Orientation = xlHidden
For Each PF In PT.DataFields
    PF.Orientation = xlHidden
Next PF

xlDataFieldの位置に2つ以上のフィールドがある場合にのみ、フィールドが存在するように見えると思います。

私を正しい方向に向けてくれたAlinbossに感謝します。私は以前にあなたの方法を試し、失敗したと確信していました-順序が重要であることがわかりました!

PSあなたのコードはまだ1つの計算されたデータフィールドだけでは機能しません

于 2014-02-17T16:29:37.693 に答える
1

Laurent Boscのコードがチェックアウトされたので、私はそれを投票しました。私の完全なコードには、すべてを非表示にした後のデータの追加が含まれています。コードはSheet1(Sheet1)に配置されます。

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
于 2016-02-19T03:37:42.180 に答える
0

計算フィールドの名前を変更しましたか?もともとは「SumofMyField」でしたか?SourceNameプロパティを確認し、それを使用して異なるかどうかを確認してください。

試しましたpt.CalculatedFields("MyField").Orientation = xlHiddenか?

于 2009-09-09T21:24:44.183 に答える
0

これはExcelのバグではないと思います。「機能」だと思います。;-)

Re:@AMissico、ピボットテーブルのすべてのフィールドを非表示にすることに問題はありませんが、彼はアイテムについて話している可能性があります。ピボットフィールドの最後のアイテムを非表示にすることはできません。

これは、あなたがやろうとしていることを行うために私が日常的に使用しているコードです。これらのマクロはExcel2002および2003で開発されました。CalculatedFieldsを非表示にせず、削除します。

' 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
于 2009-09-21T15:22:39.410 に答える
0

私はあなたとまったく同じ問題を抱えています。計算フィールドを非表示/表示するのではなく、削除して読み取る必要があるようです。

于 2009-09-24T13:41:50.250 に答える
0

計算フィールドを初めて非表示にしようとしたときに、これに対する回避策を誤って発見したので、ここで共有しようと思いました。

方向プロパティを変更する代わりに、非表示にする計算フィールドのタイトルを含むピボットテーブルのセルを選択してから、選択範囲を削除するようにコードに指示することができます。これは、テーブルに別のデータフィールドがすでに存在する限り機能します。以下の例:

シナリオ:ピボットテーブルは範囲A1:G10をカバーします。計算フィールド「Margin」はすでにテーブルにあり、データフィールド「Sales」を追加して「Margin」計算フィールドを削除します。

実行するコード:

'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

なぜこれが機能するのかはわかりませんが、少なくともこれを使用できることをうれしく思います。

于 2010-04-16T15:35:57.350 に答える
0

幸い、データフィールドを非表示にする非常に簡単な方法があります。あなたは皆、ピボットフィールドをデータフィールドと間違えていました。最初にピボットにあったピボットフィールド/データフィールドの数に関係なく、ピボットテーブルを空にするコードを提示しています。

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
于 2010-05-13T13:21:27.240 に答える
0

少し遅れていることは承知していますが、この問題はまだ自信を持って答えられておらず、有用な情報を見つけるのに苦労して同じ問題に直面していました。だから、私はこの投稿が誰かを助けるかもしれないことを願っています...

データをデータモデルに保存している場合は、ピボットフィールドの代わりにCubeFieldsを使用します。同じ問題が発生し、データモデルのない単純なブックを試してみましたが、コードは完全に機能しました(PivotFieldsを使用)。データモデルを使用したブックでエラーが返されるだけです。だから、私はこの変更とブームを行いました!機能した!私の提案は、次のコードを使用することです。

Sub PrRemove()
'remove PR
Dim pt As PivotTable
Set pt = ActiveSheet.PivotTables("MyPivot")
pt.CubeFields("MyField").Orientation = xlHidden   '<- here is the error
End Sub
于 2020-11-17T00:14:39.300 に答える
0

@ user4709164の回答のおかげで、私はこのコードを手に入れました、それは私にとって完璧に機能しています:

ピボット列はすべて軸を示すためにXまたはYで終わるので、フィールドのキャプションに最後の文字を使用します。

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
于 2021-03-09T11:30:03.593 に答える