Consulting

Results 1 to 9 of 9

Thread: do until

  1. #1

    do until

    Im erasing some lines in a sheet based on a condition wich is put in a textbox.

    Several line should be erased since they have the same name. I doing something like this,

    for i...
    if ..
    end if
    next i

    and im trying to make it run until it can find that the condition is true anywhere. This is where i stumble.

    do
    for i...
    if ..
    end if
    next i
    loop until ????

    does anybody know the solution??!

  2. #2
    Please tell me if this post i lacking info..

  3. #3
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    You already have the solution in general terms, and we can't be any more specific than what you have without knowing more detail!
    Regards,
    Rory

    Microsoft MVP - Excel

  4. #4
    Ok, here is the actual code:
    [VBA]
    Private Sub fjern_click()
    Do
    For i = 0 To 500
    If Cells(i + 9, 1).Value = Sletopg.Text Then
    Rows(i + 9).Select
    Selection.delete Shift:=xlUp

    End If
    Next i
    Loop until

    End Sub[/VBA]

    specifically the problem is what to put after the until ...

  5. #5
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    You still haven't said what the condition actually is!
    Regards,
    Rory

    Microsoft MVP - Excel

  6. #6
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    PS If you are going to delete rows, you should loop backwards - i.e. use:
    For i = 500 to 0 Step -1
    Regards,
    Rory

    Microsoft MVP - Excel

  7. #7
    thnx
    the condition to stop the loop is when there is not such Cells(i + 9, 1).Value that equals Sletopg.Text anymore

  8. #8
    Are all the cells where Cells(i + 9, 1).Value = Sletopg.Text simultaneous or are there likely to be cells where the condition is not met inbetween cells that contain sletopg.text?

    You could possible do a count of the number of times sletopg.text appears in the range and then count down from this value everytime a row is deleted and then exit the for loop when count = 0.

    Private Sub fjern_click() count = WorksheetFunction.CountIf(Range(Cells(9, 1), Cells(509, 1)), sletopg.text) For i = 500 To 0 Step -1 If Cells(i + 9, 1).Value = Sletopg.Text Then Rows(i + 9).Select Selection.delete Shift:=xlUp count = count - 1 End If If count = 0 then Exit For Next i End Sub
    I don't think a do until loop round the outside wouldn't stop the for next loop exectuing to completion
    Last edited by RECrerar; 11-30-2007 at 06:53 AM.

  9. #9
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Without a loop
    [VBA]
    Option Explicit
    Sub DelRows()
    Dim r As Range, FC As Range
    Set FC = Cells(8, 1)
    Set r = Range(FC, Cells(509, 1))
    r.AutoFilter 1, Sletopg.Text
    Set r = Range(Cells(9, 1), Cells(509, 1)).SpecialCells(xlCellTypeVisible)
    r.EntireRow.Delete
    FC.AutoFilter
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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