Iterate through an Excel dropdown/validation list

2019-06-02 06:20发布

问题:

I have a cell with a validation list that changes depending on other settings. Is it possible to iterate through that cell in code? Is it also possible to set that cell to the nth item in the list?

Example: The drop down for E2 depends on what was selected for D2. I would like to be able to iterate through whatever list is in E2 without having to check D2.

Thank you.

回答1:

This will work for you

Sub loopthroughvalidationlist()
     Dim inputRange As Range
     Dim c As Range
     Set inputRange = Evaluate(Range("D2").Validation.Formula1)
     For Each c In inputRange
        '... do something with c.Value
    Next c
End Sub