0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VBAでピポットテーブルの各要素にアクセス + フィルターにより消えたラベルにアクセスすることにより発生するエラー回避方法

Last updated at Posted at 2025-06-10
コード
ピポットテーブル作成 + 各要素アクセス + フィルターで絞って各要素アクセス
Option Explicit

Sub myPivotSub()
 
    Dim wsPivot As Worksheet
    
    Dim lastRow As Long
    Dim lastColumn As Long
    
    Dim pivotName As String
    
    Dim i As Long
    Dim j As Long
    
    Set wsPivot = ThisWorkbook.Worksheets("Pivot")
    
    ' ピポットテーブルを削除
    wsPivot.Range("A9:J20").Clear
    
    lastRow = wsPivot.Cells(wsPivot.Rows.Count, "A").End(xlUp).Row
    lastColumn = wsPivot.Cells(1, wsPivot.Columns.Count).End(xlToLeft).Column
    pivotName = "myPivot"
    
   ThisWorkbook.PivotCaches.Create(xlDatabase, wsPivot.Range(wsPivot.Cells(1, "A"), wsPivot.Cells(lastRow, lastColumn))).CreatePivotTable wsPivot.Range("A11"), pivotName

   With wsPivot.PivotTables(pivotName).PivotFields("ひらがな")
           .Orientation = xlRowField
           .Position = 1
   End With
       
   With wsPivot.PivotTables(pivotName).PivotFields("カタカナ")
           .Orientation = xlColumnField
           .Position = 1
    End With
       
    With wsPivot.PivotTables(pivotName).PivotFields("種類")
           .Orientation = xlPageField
           .Position = 1
    End With
    
    wsPivot.PivotTables(pivotName).AddDataField wsPivot.PivotTables(pivotName).PivotFields("個数"), "合計 / 個数", xlSum
    
    With wsPivot.PivotTables(pivotName)
       
        For i = 1 To .PivotFields("ひらがな").PivotItems.Count
        
            For j = 1 To .PivotFields("カタカナ").PivotItems.Count
            
                Debug.Print "「ひらがな カタカナ 個数」 " & .PivotFields("ひらがな").PivotItems(i) & " " & .PivotFields("カタカナ").PivotItems(j) & " " & _
                                     .GetPivotData("合計 / 個数", "ひらがな", .PivotFields("ひらがな").PivotItems(i), "カタカナ", .PivotFields("カタカナ").PivotItems(j)).Value
               
            Next j
            
        Next i
    
    End With
    
'    With wsPivot.PivotTables(pivotName)
'
'        With .PivotFields("種類")
'              .ClearAllFilters
'              .CurrentPage = "A"
'        End With
'
'        For i = 1 To .PivotFields("ひらがな").PivotItems.Count
'
'            For j = 1 To .PivotFields("カタカナ").PivotItems.Count
'
'On Error GoTo ErrorHandler '種類フィルターをAに絞った後、"い"や"う"がピポットテーブルに含まれないのに参照しようとしてエラーが発生するのを無視するため
'
'                  Debug.Print "「ひらがな カタカナ 個数」 " & .PivotFields("ひらがな").PivotItems(i) & " " & .PivotFields("カタカナ").PivotItems(j) & " " & _
'                                     .GetPivotData("合計 / 個数", "ひらがな", .PivotFields("ひらがな").PivotItems(i), "カタカナ", .PivotFields("カタカナ").PivotItems(j)).Value
'
'ErrorHandler:
''Debug.Print "Error: " & Err.Description
'Resume Next
'
'            Next j
'
'        Next i
'
'    End With

End Sub
実行結果
「ひらがな カタカナ 個数」 あ ア 5
「ひらがな カタカナ 個数」 あ イ 6
「ひらがな カタカナ 個数」 あ ウ 3
「ひらがな カタカナ 個数」 い ア 6
「ひらがな カタカナ 個数」 い イ 
「ひらがな カタカナ 個数」 い ウ 
「ひらがな カタカナ 個数」 う ア 
「ひらがな カタカナ 個数」 う イ 
「ひらがな カタカナ 個数」 う ウ 3

image.png
image.png

コード(フィルターで絞る箇所 + 各要素にアクセスする両箇所のコメント化を解除)
ピポットテーブル作成 + 各要素アクセス + フィルターで絞って各要素アクセス
Option Explicit

Sub myPivotSub()
 
    Dim wsPivot As Worksheet
    
    Dim lastRow As Long
    Dim lastColumn As Long
    
    Dim pivotName As String
    
    Dim i As Long
    Dim j As Long
    
    Set wsPivot = ThisWorkbook.Worksheets("Pivot")
    
    ' ピポットテーブルを削除
    wsPivot.Range("A9:J20").Clear
    
    lastRow = wsPivot.Cells(wsPivot.Rows.Count, "A").End(xlUp).Row
    lastColumn = wsPivot.Cells(1, wsPivot.Columns.Count).End(xlToLeft).Column
    pivotName = "myPivot"
    
   ThisWorkbook.PivotCaches.Create(xlDatabase, wsPivot.Range(wsPivot.Cells(1, "A"), wsPivot.Cells(lastRow, lastColumn))).CreatePivotTable wsPivot.Range("A11"), pivotName

   With wsPivot.PivotTables(pivotName).PivotFields("ひらがな")
           .Orientation = xlRowField
           .Position = 1
   End With
       
   With wsPivot.PivotTables(pivotName).PivotFields("カタカナ")
           .Orientation = xlColumnField
           .Position = 1
    End With
       
    With wsPivot.PivotTables(pivotName).PivotFields("種類")
           .Orientation = xlPageField
           .Position = 1
    End With
    
    wsPivot.PivotTables(pivotName).AddDataField wsPivot.PivotTables(pivotName).PivotFields("個数"), "合計 / 個数", xlSum
    
    With wsPivot.PivotTables(pivotName)
       
        For i = 1 To .PivotFields("ひらがな").PivotItems.Count
        
            For j = 1 To .PivotFields("カタカナ").PivotItems.Count
            
                Debug.Print "「ひらがな カタカナ 個数」 " & .PivotFields("ひらがな").PivotItems(i) & " " & .PivotFields("カタカナ").PivotItems(j) & " " & _
                                     .GetPivotData("合計 / 個数", "ひらがな", .PivotFields("ひらがな").PivotItems(i), "カタカナ", .PivotFields("カタカナ").PivotItems(j)).Value
               
            Next j
            
        Next i
    
    End With
    
    With wsPivot.PivotTables(pivotName)

        With .PivotFields("種類")
              .ClearAllFilters
              .CurrentPage = "A"
        End With

        For i = 1 To .PivotFields("ひらがな").PivotItems.Count

            For j = 1 To .PivotFields("カタカナ").PivotItems.Count

'On Error GoTo ErrorHandler '種類フィルターをAに絞った後、"い"や"う"がピポットテーブルに含まれないのに参照しようとしてエラーが発生するのを無視するため

                  Debug.Print "「ひらがな カタカナ 個数」 " & .PivotFields("ひらがな").PivotItems(i) & " " & .PivotFields("カタカナ").PivotItems(j) & " " & _
                                     .GetPivotData("合計 / 個数", "ひらがな", .PivotFields("ひらがな").PivotItems(i), "カタカナ", .PivotFields("カタカナ").PivotItems(j)).Value

'ErrorHandler:
'Debug.Print "Error: " & Err.Description
'Resume Next

            Next j

        Next i

    End With

End Sub
実行結果
「ひらがな カタカナ 個数」 あ ア 5
「ひらがな カタカナ 個数」 あ イ 6
「ひらがな カタカナ 個数」 あ ウ 3
「ひらがな カタカナ 個数」 い ア 6
「ひらがな カタカナ 個数」 い イ 
「ひらがな カタカナ 個数」 い ウ 
「ひらがな カタカナ 個数」 う ア 
「ひらがな カタカナ 個数」 う イ 
「ひらがな カタカナ 個数」 う ウ 3
「ひらがな カタカナ 個数」 あ ア 5
「ひらがな カタカナ 個数」 あ イ 6
「ひらがな カタカナ 個数」 あ ウ 3

image.png

コード(ErrorHandler箇所のコメント化を解除)
ピポットテーブル作成 + 各要素アクセス + フィルターで絞って各要素アクセス
Option Explicit

Sub myPivotSub()
 
    Dim wsPivot As Worksheet
    
    Dim lastRow As Long
    Dim lastColumn As Long
    
    Dim pivotName As String
    
    Dim i As Long
    Dim j As Long
    
    Set wsPivot = ThisWorkbook.Worksheets("Pivot")
    
    ' ピポットテーブルを削除
    wsPivot.Range("A9:J20").Clear
    
    lastRow = wsPivot.Cells(wsPivot.Rows.Count, "A").End(xlUp).Row
    lastColumn = wsPivot.Cells(1, wsPivot.Columns.Count).End(xlToLeft).Column
    pivotName = "myPivot"
    
   ThisWorkbook.PivotCaches.Create(xlDatabase, wsPivot.Range(wsPivot.Cells(1, "A"), wsPivot.Cells(lastRow, lastColumn))).CreatePivotTable wsPivot.Range("A11"), pivotName

   With wsPivot.PivotTables(pivotName).PivotFields("ひらがな")
           .Orientation = xlRowField
           .Position = 1
   End With
       
   With wsPivot.PivotTables(pivotName).PivotFields("カタカナ")
           .Orientation = xlColumnField
           .Position = 1
    End With
       
    With wsPivot.PivotTables(pivotName).PivotFields("種類")
           .Orientation = xlPageField
           .Position = 1
    End With
    
    wsPivot.PivotTables(pivotName).AddDataField wsPivot.PivotTables(pivotName).PivotFields("個数"), "合計 / 個数", xlSum
    
    With wsPivot.PivotTables(pivotName)
       
        For i = 1 To .PivotFields("ひらがな").PivotItems.Count
        
            For j = 1 To .PivotFields("カタカナ").PivotItems.Count
            
                Debug.Print "「ひらがな カタカナ 個数」 " & .PivotFields("ひらがな").PivotItems(i) & " " & .PivotFields("カタカナ").PivotItems(j) & " " & _
                                     .GetPivotData("合計 / 個数", "ひらがな", .PivotFields("ひらがな").PivotItems(i), "カタカナ", .PivotFields("カタカナ").PivotItems(j)).Value
               
            Next j
            
        Next i
    
    End With
    
    With wsPivot.PivotTables(pivotName)

        With .PivotFields("種類")
              .ClearAllFilters
              .CurrentPage = "A"
        End With

        For i = 1 To .PivotFields("ひらがな").PivotItems.Count

            For j = 1 To .PivotFields("カタカナ").PivotItems.Count

On Error GoTo ErrorHandler '種類フィルターをAに絞った後、"い"や"う"がピポットテーブルに含まれないのに参照しようとしてエラーが発生するのを無視するため

                  Debug.Print "「ひらがな カタカナ 個数」 " & .PivotFields("ひらがな").PivotItems(i) & " " & .PivotFields("カタカナ").PivotItems(j) & " " & _
                                     .GetPivotData("合計 / 個数", "ひらがな", .PivotFields("ひらがな").PivotItems(i), "カタカナ", .PivotFields("カタカナ").PivotItems(j)).Value

ErrorHandler:
'Debug.Print "Error: " & Err.Description
Resume Next

            Next j

        Next i

    End With

End Sub
実行結果(先ほど発生したエラーは発生しない)
「ひらがな カタカナ 個数」 あ ア 5
「ひらがな カタカナ 個数」 あ イ 6
「ひらがな カタカナ 個数」 あ ウ 3
「ひらがな カタカナ 個数」 い ア 6
「ひらがな カタカナ 個数」 い イ 
「ひらがな カタカナ 個数」 い ウ 
「ひらがな カタカナ 個数」 う ア 
「ひらがな カタカナ 個数」 う イ 
「ひらがな カタカナ 個数」 う ウ 3
「ひらがな カタカナ 個数」 あ ア 5
「ひらがな カタカナ 個数」 あ イ 6
「ひらがな カタカナ 個数」 あ ウ 3

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?