Consulting

Results 1 to 3 of 3

Thread: Is there a way to write this "for" loop?

  1. #1

    Is there a way to write this "for" loop?

    I need to rewrite c++ program to vba.
    I've made a function and the sub, but I dont know how to write this for loop correctly in vba
       for (pos=pos-1; a[pos] == 0; pos--)
         {
         }
    Unfortunately this dosent work as this is not a good option to write after "To" something like a(pos) = 0.
    For pos = pos - 1 To a(pos) = 0 Step -1
    Next pos
    I've got extra consultation in this thread http://www.cplusplus.com/forum/beginner/258650/
    So I need to create for loop that search first a(pos) = 0 from right to left in the array.
    It start from pos=pos-1 and ends by when a(pos) = 0. Any idea how to write this loop for vba?
    Regards,
    Alex

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    My guess
        pos = pos-1
        Do While a(pos) <> 0
    
            pos = pos -1
        Loop
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    C++ is one of the many things I don't know

    I also didn't understand the "right to left in the array" comment. Arrays have a lower bound(s) and upper bound(s)

    Here's some VBA loop-ology examples that you might be able to use


    Option Explicit
    
    Sub cPlusPlus()
        Dim i As Long
        Dim pos  As Variant
        
        pos = Array(1, 2, 3, 4, 5, 0, 6, 7, 8, 9)
    
    
        'case 1
        For i = LBound(pos) To UBound(pos)
            Debug.Print "Case 1", i, pos(i)
        Next
    
    
        'case 2
        For i = LBound(pos) To UBound(pos)
            If pos(i) = 0 Then Exit For
            Debug.Print "Case 2", i, pos(i)
        Next
    
    
        'case 3
        i = LBound(pos)
        Do While pos(i) <> 0 And i <= UBound(pos)
            Debug.Print "Case 3", i, pos(i)
            i = i + 1
        Loop
        
        'case 4
        i = LBound(pos)
        Do
            Debug.Print "Case 4", i, pos(i)
            i = i + 1
        Loop Until pos(i + 1) = 0 Or i = UBound(pos)
    
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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