Consulting

Results 1 to 7 of 7

Thread: Delete Data if Less then Yesterday

  1. #1

    Clear Rows or Cells or Delete Data if Less then Yesterday

    Is there a way to Clear or delete data from a spreadsheet Columns A thru H if let's say the date is less then Yesterday? I would prefer to clear the cells. If so How would I do that?
    Last edited by ajames420; 08-27-2007 at 07:01 AM.

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Sure, what column's the date in?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    The column the date is in is "(F4:F1625)" The date is formatted as 8/27/2007

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

    Public Sub ProcessData()
    Const TEST_COLUMN As String = "F" '<=== change to suit
    Dim i As Long
    Dim iLastRow As Long

    With ActiveSheet

    iLastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
    For i = 1 To iLastRow 'iLastRow to 1 Step -1
    If .Cells(i, TEST_COLUMN).Value < Date - 1 Then
    .Cells(i, "A").Resize(, h).ClearContents
    End If
    Next i

    End With

    End Sub
    [/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

  5. #5
    When I open up the VBA Editor where to I put that? At the top of the User Form Code or in the ThisWorkBook?

    Sorry

  6. #6
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    When you open the VBE, insert a new module, one way is to:
    Alt+I then M
    which takes you through the menus:
    Try also:[vba]Sub blah()
    For Each cll In Range("F4:F1625").Cells
    If cll.Value < (Date - 1) And IsDate(cll.Value) Then Cells(cll.Row, 1).Resize(, 8).ClearContents
    Next
    End Sub
    [/vba]You could also try replacing the If.. line with one ofthe folowing: [vba]If cll.Value < (Date - 1) And IsDate(cll.Value) Then Range(cll.Offset(, -5), cll.Offset(, 2)).ClearContents
    [/vba][vba]If cll.Value < (Date - 1) And IsDate(cll.Value) Then Range(Cells(cll.Row, 1), Cells(cll.Row, 8)).ClearContents
    [/vba]They all do the same thing in this instance but one may be easier to adapt for differing scenarios
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  7. #7
    Thank you. They all work great.

Posting Permissions

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