Results 1 to 7 of 7

Thread: can Do Until be nested inside For Next?

  1. #1
    Acolyte r121a947's Avatar
    Join Date
    Jun 2019
    Posts
    20
    Articles
    0
    Excel Version
    Office 365

    can Do Until be nested inside For Next?



    Register for a FREE account, and/
    or Log in to avoid these ads!

    Can a Do Until loop be nested inside a For Next loop?

    I have a For Next loop that was working, but when I tried to put a Do Until loop within the For Next, I get an error message "Next without For."

    Thanks, in advance.

  2. #2
    Super Moderator p45cal's Avatar
    Join Date
    Dec 2012
    Posts
    1,585
    Articles
    0
    Excel Version
    365
    Yes you can. The error implies you haven't nested properly:
    Code:
    For i = 1 To 10
      Do
        'something
      Loop Until Z = x
    Next i
    or:
    Code:
    For i = 1 To 10
      Do Until Z = x
        'something
      Loop
    Next i

  3. #3
    Acolyte r121a947's Avatar
    Join Date
    Jun 2019
    Posts
    20
    Articles
    0
    Excel Version
    Office 365
    This is the Do Until I am trying to use:

    Code:
    Do Until Cmatch = Cfind And Dmatch = Dfind And Ematch = Efind
            Cells.FindNext(After:=ActiveCell).Activate
            Exit Do
            Cells(ActiveCell.Row, 12).Value = Cells(ActiveCell.Row, 12).Value & " " & updFN
    Thanks for your interest and help.

  4. #4
    Super Moderator p45cal's Avatar
    Join Date
    Dec 2012
    Posts
    1,585
    Articles
    0
    Excel Version
    365
    Code:
    Do Until Cmatch = Cfind And Dmatch = Dfind And Ematch = Efind
      Cells.FindNext(After:=ActiveCell).Activate
    Loop
    Cells(ActiveCell.Row, 12).Value = Cells(ActiveCell.Row, 12).Value & " " & updFN
    as long as the activate code in the middle changes one or more of Cmatch, Cfind, Dmatch, Dfind, Ematch, Efind.

  5. #5
    Acolyte r121a947's Avatar
    Join Date
    Jun 2019
    Posts
    20
    Articles
    0
    Excel Version
    Office 365
    Thank you.

    Can I place additional instructions after Activate and before Loop?

  6. #6
    Super Moderator p45cal's Avatar
    Join Date
    Dec 2012
    Posts
    1,585
    Articles
    0
    Excel Version
    365
    Of course!

  7. #7
    Acolyte r121a947's Avatar
    Join Date
    Jun 2019
    Posts
    20
    Articles
    0
    Excel Version
    Office 365
    Thank you.

    Works great!

Posting Permissions

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