Consulting

Results 1 to 5 of 5

Thread: Solved: Do ... loop

  1. #1

    Question Solved: Do ... loop

    I know there are four different syntaxes of Do...Loop
    1- Do While condition [statements] Loop
    2- Do [statements] Loop While condition
    3- Do Until condition [statements] Loop
    4- Do [statements] Loop Until condition

    I only know to dela with the number 3
    [VBA]
    RowCount = 1
    Do Until Cells(RowCount, 1).Value = 5
    RowCount = RowCount + 1

    Loop
    Cells(RowCount, 1) = 10
    [/VBA]
    in this code the loop will show error if there isn't 5 number in the column 1 and if ther is a 5 will continue.

    May somone help me to understand other loop(1,2,4)

  2. #2
    VBAX Regular
    Joined
    Feb 2008
    Posts
    54
    Location
    To do the same thing with Number 1:
    [vba]RowCount = 1
    Do While Cells(RowCount, 1).Value <> 5
    RowCount = RowCount + 1

    Loop
    Cells(RowCount, 1) = 10 [/vba]

    Number 2:

    [vba]RowCount = 0
    Do
    RowCount = RowCount + 1
    Loop While Cells(RowCount, 1).Value <> 5
    Cells(RowCount, 1) = 10[/vba]

    Number 4:

    [vba]RowCount = 0
    Do
    RowCount = RowCount + 1
    Loop Until Cells(RowCount, 1).Value = 5
    Cells(RowCount, 1) = 10 [/vba]

  3. #3
    What a bout While..Wend .

  4. #4
    VBAX Regular
    Joined
    Feb 2008
    Posts
    54
    Location
    Quote Originally Posted by Nader
    What a bout While..Wend .
    "While...wend" is essentially the same as "Do While condition [statements] Loop". For your example it would be:


    [VBA]
    RowCount = 1
    While Cells(RowCount, 1).Value <> 5
    RowCount = RowCount + 1
    Wend
    Cells(RowCount, 1) = 10
    [/VBA]

    But the "Do While...Loop" method is more useful in some situations because you can use "Exit Do" to jump out of the loop. For example, if you only want to look down 100 rows for the number 5 (in your example) you would have this:

    [VBA]RowCount = 1
    Do While Cells(RowCount, 1).Value <> 5
    RowCount = RowCount + 1
    If RowCount > 100 Then Exit Do
    Loop
    Cells(RowCount, 1) = 10
    [/VBA]

    "While....Wend" doesn't have an Exit statement.

  5. #5
    Thank you for help.

Posting Permissions

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