+ Reply to Thread
Results 1 to 8 of 8

Delete Rows Multiple Criteria w/Numbers

Hybrid View

eriknokc Delete Rows Multiple Criteria... 12-23-2009, 06:17 PM
Richard Buttrey Re: Delete Rows Multiple... 12-23-2009, 06:48 PM
eriknokc Re: Delete Rows Multiple... 12-23-2009, 06:53 PM
protonLeah Re: Delete Rows Multiple... 12-23-2009, 06:59 PM
shg Re: Delete Rows Multiple... 12-23-2009, 07:11 PM
eriknokc Re: Delete Rows Multiple... 12-23-2009, 07:02 PM
eriknokc Re: Delete Rows Multiple... 12-23-2009, 07:26 PM
shg Re: Delete Rows Multiple... 12-23-2009, 07:33 PM
  1. #1
    Registered User
    Join Date
    12-05-2007
    Location
    Oklahoma
    MS-Off Ver
    2003, 2007
    Posts
    22

    Delete Rows Multiple Criteria w/Numbers

    I am trying to use the code below to eliminate data in a spreadsheet based on certain criteria. I have been able to get this to work with text fields just fine, but now I am running into an issue with numbers. I am trying to get this code to delete rows if they have the word "unknown" in column C and if the value in column B is greater than 900 hours (999:00:00), the numbers in this column are formatted as [hhh]:mm:ss. What should I tweak to make this work?

    Sub DeleteRows()
         
        Dim Last_Row As Long
        Dim iLoop As Long
        
        For iLoop = Last_Row To 1 Step -1
            If (Trim(ActiveSheet.Cells(iLoop, 2).Value) > "900") And (Trim(ActiveSheet.Cells(iLoop, 3).Value) = "Unknown") Then
                ActiveSheet.Cells(iLoop, 3).EntireRow.Delete
            End If
        Next iLoop
    End Sub
    Last edited by eriknokc; 12-23-2009 at 07:26 PM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Delete Rows Multiple Criteria w/Numbers

    Hi,

    1. Have you set the variable Last_Row somewhere?
    2. Take out the double quotes around the 900.

    However I can't help thinking that you'd be better filtering the data and then deleting the filtered rows rather than doing this with a loop. Loops are fine if the data set is relatively small, but with larger data sets you'll find they are much slower than using in built Excel functions such as data filter.

    HTH
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    12-05-2007
    Location
    Oklahoma
    MS-Off Ver
    2003, 2007
    Posts
    22

    Re: Delete Rows Multiple Criteria w/Numbers

    Yes, I forgot that part. I do have the last row set as below. I have tried removing the double quotes, but that does not work.

        Last_Row = ActiveSheet.Range("A65536").End(xlUp).Row + 1

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,936

    Re: Delete Rows Multiple Criteria w/Numbers

    Your code,
    Trim(ActiveSheet.Cells(iLoop, 2).Value) > "900"
    indicates that the values in column B are all text values. Is this the case, or do the cells contain numbers for calculations? If these are meant to be numbers and not text strings then the quotes must be deleted ( 900 is not "900"):

    ActiveSheet.Cells(iLoop, 2).Value> 900
    Also, I'm not sure from your description whether both conditions must be true or if you want the row deleted if either is true. Do you want the row deleted if either B is greater than 900 OR C is Unknown?
    Ben Van Johnson

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Delete Rows Multiple Criteria w/Numbers

    Try this:
    Sub DeleteRows()
        Dim iRow        As Long
    
        With ActiveSheet
            For iRow = .Cells(.Rows.Count, "B").End(xlUp).Row To 1 Step -1
                If .Cells(iRow, "B").Value2 > 900 / 24 And _
                   InStr(1, .Cells(iRow, "C").Value, "unknown", vbTextCompare) Then
                    .Rows(iRow).Delete
                End If
            Next iRow
        End With
    End Sub
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Registered User
    Join Date
    12-05-2007
    Location
    Oklahoma
    MS-Off Ver
    2003, 2007
    Posts
    22

    Re: Delete Rows Multiple Criteria w/Numbers

    Sorry about not being clearer on that. I was looking for it to require meet both conditions in order for it delete the row.

  7. #7
    Registered User
    Join Date
    12-05-2007
    Location
    Oklahoma
    MS-Off Ver
    2003, 2007
    Posts
    22

    Re: Delete Rows Multiple Criteria w/Numbers

    shg Forum Guru, that worked perfect! Thanks everyone for your help and insight into this.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Delete Rows Multiple Criteria w/Numbers

    You're welcome.

    I agree with Richard's comment, BTW.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1