Consulting

Results 1 to 4 of 4

Thread: Combining IF criteria in DO LOOP

  1. #1
    VBAX Regular
    Joined
    Feb 2009
    Posts
    29
    Location

    Combining IF criteria in DO LOOP

    Hi, I'm wondering how to combine IF statement criteria in a DO LOOP so I can eliminate unnecessary lines of repetitive loops...here is what I have so far:

    Range("K1").Select
    Do Until Selection.Value = ""
    If Selection.Value = "INVEST ACCOUNT CREDIT" Then
    Selection.EntireRow.Delete
    Else
    Selection.Offset(1, 0).Select
    End If
    Loop

    Range("K1").Select
    Do Until Selection.Value = ""
    If Selection.Value = "INTEREST CREDIT" Then
    Selection.EntireRow.Delete
    Else
    Selection.Offset(1, 0).Select
    End If
    Loop

    Range("K1").Select
    Do Until Selection.Value = ""
    If Selection.Value = "INVEST ACCOUNT DEBIT" Then
    Selection.EntireRow.Delete
    Else
    Selection.Offset(1, 0).Select
    End If
    Loop

    Range("K1").Select
    Do Until Selection.Value = ""
    If Selection.Value = "CONTROLLED DISBURSEMENT DEBIT" Then
    Selection.EntireRow.Delete
    Else
    Selection.Offset(1, 0).Select
    End If
    Loop

    Thanks for your help!

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Dim LastRow As Long

    With ActiveSheet

    LastRow = .Cells(.Rows.Count, "K").End(xlUp).Row
    For i = 1 To Lastrow

    If .Cells(i, "K").Value = "INVEST ACCOUNT CREDIT" Or _
    .Cells(i, "K").Value = "INTEREST CREDIT" Or _
    .Cells(i, "K").Value = "INVEST ACCOUNT DEBIT" Then

    .Rows(i).Delete
    End If
    Next i
    End With
    [/vba]
    Last edited by Bob Phillips; 02-26-2009 at 10:00 AM.
    ____________________________________________
    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 Regular
    Joined
    Feb 2009
    Posts
    16
    Location

    Question

    Quote Originally Posted by xld
    [vba]

    Dim LastRow As Long

    With ActiveSheet

    LastRow = .Cells(.Rows.Count, "K").End(xlUp).Row
    For i = 1 To laqstrow

    If .Cells(i, "K").Value = "INVEST ACCOUNT CREDIT" Or _
    .Cells(i, "K").Value = "INTEREST CREDIT" Or _
    .Cells(i, "K").Value = "INVEST ACCOUNT DEBIT" Then

    .Rows(i).Delete
    End If
    Next i
    End With
    [/vba]
    Shouldn't the loop be
    [VBA]For i = LastRow to 1 Step -1[/VBA]
    Otherwise when row x is deleted row x+1 will become row x and will not be checked since i will be incremented to x+1.

  4. #4
    VBAX Regular
    Joined
    Feb 2009
    Posts
    29
    Location
    Thank you xld and Sagy!

    Sagy, your change was correct. The initial way did neglect a few lines, but " i=LastRow to 1 Step -1 " fixed it. Thanks again!

Posting Permissions

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