Consulting

Results 1 to 9 of 9

Thread: Solved: Mulitple If function?

  1. #1

    Solved: Mulitple If function?

    Hi all, I am trying to do a loop which deletes a row of data containing mulitple strings

    [VBA] For U = 2 To outRow
    If Cells(U, 5).Value = "Blue" Or Cells(U, 5).Value = "Purple" Or Cells(U, 5).Value = "Pink" Then
    Rows(U).Delete
    End If
    Next[/VBA]

    But it doesn't seem to work. Does anyone know what is happening?

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

    For U = outRow To 2 Step -1
    If Cells(U, 5).Value = "Blue" Or Cells(U, 5).Value = "Purple" Or Cells(U, 5).Value = "Pink" Then
    Rows(U).Delete
    End If
    Next U
    [/vba]
    ____________________________________________
    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
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    What is happening that you don't like? (Or isn't that you do?)

    Your test is case sensitive.

  4. #4
    Hi, yeah, i've tried to programme my code so that it deletes the row, if that cell in that column 5 (E), contains the following words:

    Blue
    Purple
    Pink

    with outrow being the last row found in my list.

    but the code doesn't seem to work, i'm not sure if this is the way to write an if statement with mulitple conditions in VBA?

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Contains or equals?
    ____________________________________________
    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

  6. #6
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    alivenwell

    Did you try xld's code?

    The principle is that whenever deleting you work backwards.

  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Select Case is handy if you have a range of values to test.
    [VBA]Sub DelRows()
    For U = outRow To 2 Step -1
    Select Case UCase(Cells(U, 5))
    Case "BLUE", "PURPLE", "PINK"
    Rows(U).Delete
    End Select
    Next
    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'

  8. #8
    Yeah, the code works.....completely bewildered that it was deleting the row and then moving on. Thanks for all your help guys.

  9. #9
    You are bewildered that for a value of 5 that met your condition row 5 was deleted, making row 6, row 5 then you increment your value of 5 to 6 and are bewildered as to why the original row 6 was never tested ????
    2+2=9 ... (My Arithmetic Is Mental)

Posting Permissions

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