简体   繁体   中英

Excel 2010 VBA - check if slicer value exists

I have a spreadsheet looking at multiple different details of an organisation. All fields are different in the different datasets apart from 'costcentre' which is present in all datasets.

each dataset has its own set of pivot tables and slicers. I am looking to make the costcentre slicer for each table match a chosen slicer.

I have made it work when both slicers have the same values but when the slicer being changed does not have the value of the chosen slicer it is selecting all values. Therefore I am trying to get it to check if a value exists before changing its state and getting it to move onto the next one if it does not (multiple values can be selected) but I cannot find a way of simply checking if a value exists and if not then move on to the next value in the for each bit of the code. Any advice on how to do that or a simpler way of achieving the original goal would be much appreciated

Below is my VBA code. Please note I don't actually have any experience of VBA, this has been constructed from searching for answers on this site and copying bits that seemed relevant with a little bit of trial and error thrown in.

Sub test()
Dim sc1 As SlicerCache
Dim sc2 As SlicerCache
Dim si1 As SlicerItem
Dim si2 As SlicerItem

Set sc1 = ThisWorkbook.SlicerCaches("Slicer_CC1")
Set sc2 = ThisWorkbook.SlicerCaches("Slicer_CC")

Application.ScreenUpdating = False
Application.EnableEvents = False

sc2.ClearManualFilter



For Each si1 In sc1.SlicerItems
        sc2.SlicerItems(si1.Name).Selected = si1.Selected
        On Error Resume Next
Next si1


    MsgBox "Update Complete"

clean_up:
        Application.EnableEvents = True
        Application.ScreenUpdating = True
        Exit Sub

err_handle:
        MsgBox Err.Description
        Resume clean_up
End Sub

For starters: Altering the part with the loop as follows will not generate errors. If the item from slicer1 exists in the one you are updating, it will take the selection from the first slicer based on the name.

On Error Resume Next
For Each si1 In sc1.SlicerItems
   Set si2 = sc2.SlicerItems(si1.Name)
   If Not si2 Is Nothing Then
        si2.Selected = si1.Selected
    End If
Next si1
On Error GoTo 0

However, items that are in the second slicer that aren't available in the first one, will be selected no matter what in this scenario, since you cleared the initial selection using sc2.ClearManualFilter

If you wish to invert that behaviour, use:

On Error Resume Next
For Each si2 In sc2.SlicerItems
    Set si1 = sc1.SlicerItems(si2.Name)
    If Not si1 Is Nothing Then
        si2.Selected = si1.Selected
    Else
        si2.Selected = False
    End If
Next si2
On Error GoTo 0

In this case, if nothing is selected in the second slicer (since there are no matching items), no filter will be applied (select all).

Of course you can also leave out the sc2.ClearManualFilter line: That way you will only trigger each item to be equal to slicer1 with the first option above. The rest of the selection will then stay as it was before the script was executed.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM