Excel VBA循环通过数据透视表项

时间:2022-09-15 20:46:43

I want to loop through my pivot items and check if they exist in another table, see my Example Screenshot:

我想遍历我的数据透视表项并检查它们是否存在于另一个表中,请参阅我的示例屏幕截图:

Excel VBA循环通过数据透视表项

So i want to loop through all the colors, checking if they exist in another table (e.g. in another sheet or so):

所以我想遍历所有颜色,检查它们是否存在于另一个表格中(例如在另一张表格中):

Excel VBA循环通过数据透视表项

Is there any way to do this so there would appear a Message Box that the color purple was not found in the list?

有没有办法这样做,所以会出现一个消息框,在列表中找不到紫色?

Many thanks for your help!

非常感谢您的帮助!

1 个解决方案

#1


6  

You can use something like this:

你可以使用这样的东西:

Sub ListMissingItems()

    Dim pt As PivotTable
    Dim pf As PivotField
    Dim pi As PivotItem
    Dim rngList As Range
    Dim strMsg As String

    ' change sheet and range
    Set rngList = Worksheets("List").Range("A1:A10")

    Set pt = ActiveSheet.PivotTables(1)
    ' change field as needed
    Set pf = pt.PivotFields("Colour")

    For Each pi In pf.PivotItems
        If IsError(Application.Match(pi.Caption, rngList, 0)) Then strMsg = strMsg & vbLf & pi.Caption
    Next pi

    If Len(strMsg) > 0 Then MsgBox "The following items were not found in the list:" & strMsg
End Sub

#1


6  

You can use something like this:

你可以使用这样的东西:

Sub ListMissingItems()

    Dim pt As PivotTable
    Dim pf As PivotField
    Dim pi As PivotItem
    Dim rngList As Range
    Dim strMsg As String

    ' change sheet and range
    Set rngList = Worksheets("List").Range("A1:A10")

    Set pt = ActiveSheet.PivotTables(1)
    ' change field as needed
    Set pf = pt.PivotFields("Colour")

    For Each pi In pf.PivotItems
        If IsError(Application.Match(pi.Caption, rngList, 0)) Then strMsg = strMsg & vbLf & pi.Caption
    Next pi

    If Len(strMsg) > 0 Then MsgBox "The following items were not found in the list:" & strMsg
End Sub