Consulting

Results 1 to 14 of 14

Thread: Delete row based upon Date

  1. #1

    Delete row based upon Date

    Hi all.

    In column F, starting from row 11, I have a list of dates.

    Does anyone know how to create a VBA that will delete the entire row the date is in if the date is a week old... " =Today()-7 " is the cell function for this if that helps.

    Thanks a lot in advance for anyone that can help.

  2. #2
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Sub VBAX_SamT_DeleteWeekOld()
    Dim r As Long
    
    'Must delete Rows from bottom up
    For r = Cells(Rows.Count, "F").End(xlUp).Row to 11 Step - 1
       If CDate(Cells(r, "F")) < Date - 7 Then Rows(r).Delete
    Next r
    End Sub
    Last edited by Aussiebear; 02-20-2016 at 01:33 AM. Reason: Added hash tag to code
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,077
    Location
    @ Sam. Just wondering your formula is incorrect? Should it be ">Date-7 " to be a week old?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    Quote Originally Posted by SamT View Post
    Sub VBAX_SamT_DeleteWeekOld()
    Dim r As Long

    'Must delete Rows from bottom up
    For r = Cells(Rows.Count, "F").End(xlUp).Row to 11 Step - 1
    If CDate(Cells(r, "F")) < Date - 7 Then Rows(r).Delete
    Next r
    End Sub
    Hi SamT thank you so much for this!

    If you wouldn't mind answering a further question:
    I understand what the script is doing (deleting all rows below f11 with old dates), however this is creating a problem as it is deleting empty rows and ruining the format.

    Is it possible for the script to search for the last entry in column F (starting at f11), use this as the range, and then only delete rows within this range where the criteria applies? I.e if the the last entry was in F17 the script would recognize this, use f11-f17 as the range and then delete the dates within this.

    Thanks in advance, really appreciate it.

  5. #5
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    You have data below the dates? Oops.
    If there are no empty cells in the dates range in column F...

    Change
    Cells(Rows.Count, "F").End(xlUp).RowCells(Rows.Count, "F").End(xlUp).Row
    to
    Range("F11").End(xldown).Row

    IF Column F has empty cells in the table Change the Range Column to one that will never have empty cells.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  6. #6
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Ted, > Date - 7 is newer than a week. < Date - 7 is older than a week.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  7. #7
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,077
    Location
    Yep Sam, this is why I questioned your reply. The OP is asking if a date is a week old, not less than a week.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  8. #8
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    That's what I gave. Older than one week

    Today is the 16th.
    To delete the 3rd use < 16 - 7 [3 is less than 9]
    To delete the 10th use > 16 - 7 [10 is greater than 9]
    To delete the 9th use =< 16 - 7 or < 16 - 6

    I left it to the OP to decide if they want to keep Rows that are 8 days old (= Date - 7) because the original formula they gave was "Older than one week and one day."
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  9. #9
    VBAX Regular
    Joined
    Feb 2016
    Location
    Bangalore,India
    Posts
    11
    Location
    You can use a worksheetfunction.weeknumber formula with do until loop/For loop

    ex:
    sub delet_code()

    range("f11").select
    do until activecell.value=""

    if worksheetfunction.weeknumber(activecell.value)<worksheetfunction.weeknumber (Date) then
    activecell.entirerow.delete
    else
    activecell.offset(1,0).select
    endif

    loop

    End sub

  10. #10
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,077
    Location
    Since I'm in my natural state of confusion...... which is in't hard to do. Lets assume that a date in (r, F) could potentially be high 20's and the next week could be low single digits how does your code work Sam?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  11. #11
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Dates, and Date+Time),) are stored as doubles. Date are actually the number of days from a certain date in the past (1/1/1900 or 1/1/1904, usually 1904.) Times are the decimal fraction of a day. 6AM = .25, Noon = .5, 6Pm = .75) One minute = 1/24*60, One Second = 1/24*60*60

    Today, 2/19/16 is day 1/1/04, (Day Zero) + 42419 days
    MsgBox Format(0, "mm-dd-yy")
    MsgBox Format(42419, "mm-dd-yy")

    Enter a date in a Cell, format the next cell as a Number. Copy the Date cell and PasteSpecial Values into the Number cell.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  12. #12
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,077
    Location
    I've just spent the day out on the water (burnt, tired and nil fish), on opening this thread I have to agree your logic is good.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  13. #13
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    No Fish! Blame EL Nino!

    How's your navigation spreadsheet working?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  14. #14
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,077
    Location
    Sigh...... none to well but that another topic
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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