+ Reply to Thread
Results 1 to 7 of 7

Delete Rows Based on Time criteria

  1. #1
    Registered User
    Join Date
    01-30-2012
    Location
    Hasselt, Belgium
    MS-Off Ver
    Excel 2011 for Mac
    Posts
    4

    Delete Rows Based on Time criteria

    Hi,

    I'm trying to solve a problem i have with my macro code for deleting entire rows when cell values in column V
    are between 23:00:00 and 08:00:00 o'clock. The cells in column V are formatted as 'time' and the number of rows in the sheet
    must be able to change.

    The code worked fine for another
    similar sheet with time criteria between 19:00:00 and 08:00:00. I can't seem to figure out what the problem is here..

    The code i have right now is:

    Sub Delete_rows

    Sheets("New").Select

    For i = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
    If Cells(i, "V") > "23:00:00" And Cells(i, "V") < "8:00:00" Then
    ActiveSheet.Cells(i, "A").EntireRow.Delete
    Cells(i - 1, 1).Select
    End If
    Next i

    End Sub

    Can anyone help me with this one?

    Thanks a lot!
    Last edited by Roelandu; 01-30-2012 at 11:12 AM.

  2. #2
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Delete Rows Based on Time criteria

    Try this:

    Please Login or Register  to view this content.
    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Registered User
    Join Date
    01-30-2012
    Location
    Hasselt, Belgium
    MS-Off Ver
    Excel 2011 for Mac
    Posts
    4

    Re: Delete Rows Based on Time criteria

    Thanks Dom. When i apply this code, it deletes everything but row 1 (the header). I think the OR operator
    is not the best one anyway because both conditions (>23h and <8h) need to be fulfilled, so i'd rather go for AND even
    though that doesn't work either.

    I'm quite desperate, i've been trying stuff for hours but i can seem to make it work
    I'm definitly not an expert in this kind of codes.

    Any help is welcome!

  4. #4
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Delete Rows Based on Time criteria

    A single time value can't really be greater than 23:00 and less the 08:00 so Or would be the operator I think. Can you upload a sample file?

    Dom

  5. #5
    Registered User
    Join Date
    01-30-2012
    Location
    Hasselt, Belgium
    MS-Off Ver
    Excel 2011 for Mac
    Posts
    4

    Re: Delete Rows Based on Time criteria

    Yes you're right! It should be Or. I figured it out. My time format was "1:00:00" for 1-digit hours. I changed it to "01:00:00" and now your code works fine.
    I didn't use the TimeSerial(), it also works with the time between "".

    The code:

    Sub DeleteRows()

    Dim i As Long
    With Sheets("New")
    For i = .Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
    If .Cells(i, "V") > "23:00:00" Or .Cells(i, "V") < "08:00:00" Then
    .Rows(i).Delete
    End If
    Next i
    End With
    End Sub


    Thanks a lot man! You saved me

  6. #6
    Registered User
    Join Date
    05-08-2011
    Location
    India, Mumbai
    MS-Off Ver
    Excel 2010
    Posts
    59

    Re: Delete Rows Based on Time criteria

    Can it be applicable if column V is date & time (3/9/2012 12:00:00 AM), i want to delete rows based on time only date may be different / any one

    RK

  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Delete Rows Based on Time criteria

    rvkadu1,

    Welcome to the Forum, unfortunately:

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread. It makes sense to have a new thread for your question because a thread with numerous replies can be off putting & difficult to pick out relevant replies.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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