+ Reply to Thread
Results 1 to 10 of 10

Find rows and delete them based on multiple criteria

  1. #1
    Forum Contributor
    Join Date
    03-21-2007
    Posts
    118

    Find rows and delete them based on multiple criteria

    Hi, i've searched the forum but couldn't find exactly what I'm after.

    I have a macro that looks into a worksheet, filters a column and has to delete rows.

    I want to delete those rows based on a set of criteria. Since the criteria have to do with Time, I need it to be over xx:00:00 or under xx:00:00 etc.

    Below is the macro and the criteria I want to plug in the "strCriteria =" bit is:
    delete all rows where the value is: ">19:00:00 and <07:00:00" or ">10:00:00 and <16:00:00", so basically keep only the rows with data between the hours 7-10am and 4-7pm (I have the hours in number format, just wrote them here as hours to make it simple to understand).

    Any suggestions will be greatly appreciated.

    macro:
    Option Explicit

    Sub FilterData()
    ''''''''''''''''''''''''''
    'Written by www.ozgrid.com
    ''''''''''''''''''''''''''

    Dim rRange As Range
    Dim strCriteria As String
    Dim lCol As Long
    Dim rHeaderCol As Range
    Dim xlCalc As XlCalculation
    Const strTitle As String = "OZGRID CONDITIONAL ROW DELETE"

    On Error Resume Next
    Step1:
    'We use Application.InputBox type 8 so user can select range
    Set rRange = Range("a1:d16000")



    Step2:
    'We use Application.InputBox type 1 so return a number
    lCol = 2

    Step3:
    'We use default InputBox type as we want Text
    strCriteria = Range("c3")


    'Store current Calculation then switch to manual.
    'Turn off events and screen updating
    With Application
    xlCalc = .Calculation
    .Calculation = xlCalculationManual
    .EnableEvents = False
    .ScreenUpdating = False
    End With


    'Remove any filters
    ActiveSheet.AutoFilterMode = False

    With rRange 'Filter, offset(to exclude headers) and delete visible rows
    .AutoFilter Field:=lCol, Criteria1:=strCriteria
    .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    End With

    'Remove any filters
    ActiveSheet.AutoFilterMode = False

    'Revert back
    With Application
    .Calculation = xlCalc
    .EnableEvents = True
    .ScreenUpdating = True
    End With
    On Error GoTo 0
    End Sub
    Last edited by kostas; 01-31-2012 at 11:46 AM.
    _-= Have you google'd your question before posting? =-_
    _-= Have you Searched the forum for an answer before posting? =-_

  2. #2
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Find rows and delete them based on multiple criteria

    a value can never be greater than 19:00 and less than 7:00!
    ">19:00:00 and <07:00:00"
    Regards,
    Khaled Elshaer
    www.BIMcentre.com

    Remember To Do the Following....
    1. Thank those who have helped you by clicking the Star below their post.
    2. Mark your post SOLVED if it has been answered satisfactorily:
    • Select Thread Tools (on top of your 1st post)
    • Select Mark this thread as Solved

  3. #3
    Forum Contributor
    Join Date
    03-21-2007
    Posts
    118

    Re: Find rows and delete them based on multiple criteria

    Quote Originally Posted by Kelshaer View Post
    a value can never be greater than 19:00 and less than 7:00!
    ">19:00:00 and <07:00:00"
    Yes you're right, my mistake. I need it to be >19:00:00 OR <07:00:00 in that case

  4. #4
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Find rows and delete them based on multiple criteria

    in which column does the time values appear?

  5. #5
    Forum Contributor
    Join Date
    03-21-2007
    Posts
    118

    Re: Find rows and delete them based on multiple criteria

    Quote Originally Posted by Kelshaer View Post
    in which column does the time values appear?
    Column B (that's why i got icol=2)

  6. #6
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Find rows and delete them based on multiple criteria

    Well, I assumed the times reside in Column B. If not then replace it with whatever you want in the below code.

    Please Login or Register  to view this content.
    Last edited by Kelshaer; 01-31-2012 at 09:54 AM.

  7. #7
    Forum Contributor
    Join Date
    03-21-2007
    Posts
    118

    Re: Find rows and delete them based on multiple criteria

    Quote Originally Posted by Kelshaer View Post
    Well, I assumed the times reside in Column B. If not then replace it with whatever you want in the below code.
    Thanks, this seems to be working but it's taking a long time as I've got '000s of rows. Isn't there any way to amend the macro in OP as it uses the Filter which speeds things up? It works when I type one criterion, but not for multiple ones.
    EDIT: Something seems to be wrong in the macro you posted with how it handles time as it whittled down 15000 rows to about 80!. when there should be a few thousand.

  8. #8
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Find rows and delete them based on multiple criteria

    Try this

    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    03-21-2007
    Posts
    118

    Re: Find rows and delete them based on multiple criteria

    Quote Originally Posted by Kelshaer View Post
    Try this
    This is much faster due to the screen not updating. The speed is adequate but it seems to remove way too many results, I'm only left with about 80 rows out of 12,000.

    The data I got in column be is in the following format:
    08:39:03
    05:44:22
    etc.

    Perhaps I need to specify data strings in the macro to represent the time values?

    Thank you very much for your help so far.

    EDIT: I've figured out where is the issue with your code. It's the 60*vba.minute that causes the trouble as it makes the macro to delete even rows with time data between 7am-10am. Imagine 7:15:00, with 60*minutes it makes the hour element go over 10am, then deleted. That's why the only values left are the hh:00:00 (0 minutes). Any ways around this? I think I can make it >= or <= to get rid of this problem.

    EDIT2:

    I've edited the code to this:
    Please Login or Register  to view this content.
    For some reason it deletes all time values after 10am. All I get is 7am-10am.

    EDIT3: Fixed! For some reason my column had not all the values converted to time format and that's why the >10am kept getting deleted.

    Thanks for all the help.
    Last edited by kostas; 01-31-2012 at 11:46 AM.

  10. #10
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Find rows and delete them based on multiple criteria

    Sorry for the mistake
    Use this instead as it will delete a record that is for example 7:05 PM
    The old one (After your modification) will only consider the hour value and disregard the minutes.
    Please Login or Register  to view this content.

+ 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