+ Reply to Thread
Results 1 to 6 of 6

Macro, autofilter, delete rows outside date range, defined names as criteria

Hybrid View

rain4u Macro, autofilter, delete... 04-25-2011, 07:58 PM
nimrod Re: Macro, autofilter, delete... 04-25-2011, 08:12 PM
rain4u Re: Macro, autofilter, delete... 04-25-2011, 08:17 PM
rain4u Re: Macro, autofilter, delete... 04-27-2011, 12:33 AM
nimrod Re: Macro, autofilter, delete... 04-27-2011, 11:25 AM
rain4u Re: Macro, autofilter, delete... 04-30-2011, 04:10 AM
  1. #1
    Forum Contributor
    Join Date
    01-29-2011
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    471

    Macro, autofilter, delete rows outside date range, defined names as criteria

    Hi. I have two named cells that contain dates:
    figures_date_Monday
    figures_date_Friday
    I would like to have a macro using auto filter to delete rows that do not fall between those dates. Monday as start date and Friday as end date (both inclusive). Maybe this code below that I found on the Internet can be adjusted to do this task:
    Sub keep_dates()
        Dim stStart As String, stEnd As String
        Dim dbStart As Double, dbEnd As Double
    
        Application.ScreenUpdating = 0
    
        stStart = InputBox("Please supply a start date", "Date Input", Date)
        stEnd = InputBox("Please supply an end date", "Date Input", Date)
    
        If Not IsDate(stStart) Or Not IsDate(stEnd) Then
            MsgBox "Invalid Dates", vbExclamation, "Input Error"
            GoTo ExitSub
        End If
    
        dbStart = CDbl(CDate(stStart))
        dbEnd = CDbl(CDate(stEnd))
    
        With ActiveSheet.Columns(9)
            .AutoFilter Field:=1, Criteria1:="<" & dbStart, Operator:=xlOr, Criteria2:=">" & dbEnd
            .Resize(Rows.Count - 1).Offset(1).SpecialCells(12).EntireRow.Delete
            If .Parent.AutoFilterMode = True Then .AutoFilter
        End With
        
    ExitSub:
        Application.ScreenUpdating = 1
    End Sub
    This code above prompts with input window box to allow user to manually write the dates but remember I would like to use named cells instead.


    Any ideas

    Cheers
    Rain
    Last edited by rain4u; 04-30-2011 at 04:11 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Macro, autofilter, delete rows outside date range, defined names as criteria

    What column are the dates in ? Is the sheet the activesheet ? Do you have a header row that should be excluded from inspection ?

  3. #3
    Forum Contributor
    Join Date
    01-29-2011
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    471

    Re: Macro, autofilter, delete rows outside date range, defined names as criteria

    Yes I forgot to mention these details. Active sheet, column 9 ( or column col i) and it does have header row (row1).

    Cheers
    Rain

  4. #4
    Forum Contributor
    Join Date
    01-29-2011
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    471

    Re: Macro, autofilter, delete rows outside date range, defined names as criteria

    *bump*bump*

  5. #5
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Macro, autofilter, delete rows outside date range, defined names as criteria

    Assumptions:
    -- All values in Column "i' , except for row 1, are dates

    Public Sub DeleteRowByDateRange()
       Dim StartDate As Date, EndDate As Date
       
       'Config Here
       Const DateColumn As String = "I"
       
       StartDate = Range("figures_date_Monday").Value
       EndDate = Range("figures_date_Friday").Value
    
       For iRow = Cells(Rows.Count, DateColumn).End(xlUp).Row + 1 To 2 Step -1
          If Cells(iRow, DateColumn).Value < StartDate Or Cells(iRow, DateColumn).Value > EndDate Then
             Cells(iRow, DateColumn).EntireRow.Delete
          End If
          
       Next iRow
    
    End Sub

  6. #6
    Forum Contributor
    Join Date
    01-29-2011
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    471

    Re: Macro, autofilter, delete rows outside date range, defined names as criteria

    Thx Nimrod. It works. I also learned from your code. If someone else needs to get the same result but get it by using autofilters then the following would do the job
    Sub keep_dates()
        Dim dbStart As Double, dbEnd As Double
        Application.ScreenUpdating = 0
       dbStart = Range("figures_date_Monday").Value
       dbEnd = Range("figures_date_Friday").Value
    
    With ActiveSheet.Columns(9)
            .AutoFilter Field:=1, Criteria1:="<" & dbStart, Operator:=xlOr, Criteria2:=">" & dbEnd
            .Resize(Rows.Count - 1).Offset(1).SpecialCells(12).EntireRow.Delete
            If .Parent.AutoFilterMode = True Then .AutoFilter
        End With
    ExitSub:
        Application.ScreenUpdating = 1
    End Sub

    Cheers
    Rain

+ 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