Consulting

Results 1 to 13 of 13

Thread: Loop values from data validation list vba

  1. #1
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location

    Loop values from data validation list vba

    Hello,
    I have a validation data in "C3" with the following structure:

     =INDEX(INDIRECT(B3);;1)
    indirect() function will take the name of table (created as an range) and from that, table column 1 will become an drop down data validation list.
    But now, I like to go from those values one by one (loop).

    How can I do this?
    Thanks in advance!

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Sub test()
        Dim v
    
        v = Evaluate(Range("C3").Validation.Formula1)
        MsgBox Join(WorksheetFunction.Transpose(v), vbLf)
    
    End Sub

  3. #3
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    If you attach a file, I could help probably.

  4. #4
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    Dim rng As Range, cell As Range
    
    
    Set rng = Evaluate(ActiveSheet.Range("C3").Validation.Formula1)
    error 1004

  5. #5
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    see attachment!
    Attached Files Attached Files

  6. #6
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Sub test2()
        Dim v
         
        v = Evaluate(Range("C3").Validation.Formula1)
        With Worksheets.Add
            .Cells(1).Resize(UBound(v)).Value = v
        End With
         
    End Sub
    
    
    Sub test3()
        Dim v
        Dim i As Long
         
        v = Evaluate(Range("C3").Validation.Formula1)
        For i = LBound(v, 1) To UBound(v, 1)
            MsgBox v(i, 1)
        Next
         
    End Sub

  7. #7
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    on

    Evaluate(Range("C3").Validation.Formula1)
    I have error 2015

  8. #8
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Sub Main()  
      Dim c As Range
      For Each c In Range(Range("B3").Value2)
         'Debug.Print c.Address, c.Value
         Debug.Print c.Address(External:=True), c.Value
      Next c
    End Sub

  9. #9
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    @Kenneth
    I have an Error 1004


    I see that vba canot evaluate:
    Application.Evaluate("=INDEX(INDIRECT(" & ActiveSheet.Range("C3") & " );;1)")

  10. #10
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    @Kenneth


    I do not have an error (my bad) ... But the loop is going to all table. It is possible to go just on the first column?

  11. #11
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    I putted like this. (it is working)... What do you think?
    Sub Main()
        Dim c As Range
        For Each c In Range(Range("B3").Value2)
            If c.Column = 1 Then Debug.Print c.Value
        Next c
    End Sub

  12. #12
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Sure. e.g.
    Sub Main()    
      Dim c As Range
        For Each c In WorksheetFunction.Index(Range(Range("B3").Value2), 0, 1)
            Debug.Print c.Address(External:=True), c.Value
        Next c
    End Sub
    Yes, you can do that but adds an extra step and thus time. For say < 100,000 rows, it probably won't make much difference in time.

    Of course there are other ways if all you wanted were the values. I would use array methods. Howsoever, 2 solutions should suffice. There are most always many ways to do these things. Some are a bit faster but the time spent to make a faster routine may or may not pay off.

  13. #13
    VBAX Mentor
    Joined
    Dec 2009
    Posts
    416
    Location
    You are correct!

    In the end I putted like this:

    Sub Main()
        Dim c As Range
        For Each c In Range(Range("B3").Value).Columns(1).Cells
            Debug.Print c.Address(External:=True), c.Value
        Next c
    End Sub
    Thanks for your help and time! Have a nice day

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •