+ Reply to Thread
Results 1 to 8 of 8

Delete dates not within a date range

Hybrid View

  1. #1
    Registered User
    Join Date
    12-07-2012
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    13

    Question Delete dates not within a date range

    Need to delete the dates before a start date
    Need to delete the dates after a end date
    Need help modifying the code below.

    Start Date
    8/14/2013
    8/23/2013
    8/23/2013
    8/23/2013
    9/1/2013
    9/1/2013
    9/5/2013
    9/13/2013
    9/20/2013
    9/24/2013
    9/29/2013

    startdate = InputBox("Enter the start date of your date range in mm/dd/yy format")
    mystartdate = CLng(startdate)
    enddate = InputBox("Enter the end date of your date range in mm/dd/yy format")
    myenddate = CLng(enddate)
    myendrow = ActiveSheet.Range("F" & Rows.Count).End(xlUp).Row
    For i = myendrow To 2 Step -1
    If Cells(i, 1) < mystartdate And Cells(i, 1) > myenddate Then
    Cells(F, 2).EntireRow.Delete
    End If
    Next i
    
    End Sub

    The "Test program Equipment Planner2.xls" transfer data from "PMData.xls"
    Attached Files Attached Files
    Last edited by alansidman; 11-11-2013 at 09:58 PM. Reason: code tags added

  2. #2
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Delete dates not within a date range

    Maybe
    Sub RemoveDates()
    Dim rng As Range
    Dim dStart As Date
    Dim dEnd As Date
    dStart = CDate(Application.InputBox("Enter start date in format DD/MM/YYYY", "Start Date"))
    dEnd = CDate(Application.InputBox("Enter end date in format DD/MM/YYYY"))
    If IsDate(dStart) = False Or IsDate(dEnd) = False Then
        MsgBox "You did not enter date in correct format, please try again."
        Exit Sub
    End If
    For Each rng In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
        If CDate(rng.Value) < dStart Or CDate(rng.Value) > dEnd Then
            rng.EntireRow.Delete
        End If
    Next rng
    
    End Sub
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  3. #3
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: Delete dates not within a date range

    Hello,

    Try this code :
    Sub test()
       Dim startdate As String
       Dim enddate As String
       Dim mystartdate As Long
       Dim myenddate As Long
       Dim myendrow As Long
       Dim i As Long
    
       startdate = InputBox("Enter the start date of your date range in mm/dd/yy format")
       mystartdate = CDate(startdate)
       enddate = InputBox("Enter the end date of your date range in mm/dd/yy format")
       myenddate = CDate(enddate)
       myendrow = ActiveSheet.Range("F" & Rows.Count).End(xlUp).Row
       For i = myendrow To 2 Step -1
          If CDate(Cells(i, 1)) < mystartdate Or CDate(Cells(i, 1)) > myenddate Then
             Rows(i).EntireRow.Delete
          End If
       Next i
    
    End Sub

    Reminder : As per forum rules. Please use code tags to make your code visible.
    Last edited by alansidman; 11-11-2013 at 09:55 PM.
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Delete dates not within a date range

    Yet one more:

    Sub Delete_Excluded_Dates()
        
        Dim LastRow As Long, strDate As String, lStart As Long, lEnd As Long
        
        LastRow = Range("F" & Rows.Count).End(xlUp).Row
        
        Do
            strDate = Application.InputBox("Enter the start date of your date range in mm/dd/yy format", "Start Date", Type:=2)
            If strDate = "False" Then Exit Sub  'User canceled
            If IsDate(strDate) Then Exit Do
            MsgBox "Invalid date entry. ", , "Invalid Entry"
        Loop
        lStart = CDate(strDate)
        
        Do
            strDate = Application.InputBox("Enter the end date of your date range in mm/dd/yy format", "End Date", Type:=2)
            If strDate = "False" Then Exit Sub  'User canceled
            If IsDate(strDate) Then Exit Do
            MsgBox "Invalid date entry. ", , "Invalid Entry"
        Loop
        lEnd = CDate(strDate)
        
        Application.ScreenUpdating = False
        Range("F1:F" & LastRow).AutoFilter 1, "<" & lStart, xlOr, ">" & lEnd
        Range("F2:F" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        ActiveSheet.AutoFilterMode = False
        Application.ScreenUpdating = True
        
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  5. #5
    Registered User
    Join Date
    12-07-2012
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Delete dates not within a date range

    Thank you AlphaFrog

    When I run the I get a "runtime error 1004" at this line

    "Range("F2:F" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete".
    I have attach my files to this thread.
    Last edited by alansidman; 11-11-2013 at 09:57 PM.

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Delete dates not within a date range

    Quote Originally Posted by BowHunter09 View Post
    Thank you AlphaFrog

    When I run the I get a "runtime error 1004" at this line

    "Range("F2:F" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete".
    I have attach my files to this thread.
    Try this...

        Application.ScreenUpdating = False
        ActiveSheet.AutoFilterMode = False
        Range("F1:F" & LastRow).AutoFilter 1, "<" & lStart, xlOr, ">=" & lEnd + 1
        Range("F2:F" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        ActiveSheet.AutoFilterMode = False
        Application.ScreenUpdating = True
    Last edited by AlphaFrog; 11-12-2013 at 12:14 AM.

  7. #7
    Registered User
    Join Date
    12-07-2012
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Delete dates not within a date range

    Thank you very much, it works.

  8. #8
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,719

    Re: Delete dates not within a date range

    @BowHunter09

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (I have added them for you this time. Please read all the forum rules and observe them in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Popup box asking the effective date, so macro can delete any dates < entered date.
    By shiva_reshs in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-07-2013, 07:40 AM
  2. Date filter, find listed dates and delete whole row
    By zit1343 in forum Excel General
    Replies: 3
    Last Post: 04-25-2012, 08:16 PM
  3. Macro to delete dates > xx date
    By koltregaskes in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-12-2011, 10:35 AM
  4. Replies: 8
    Last Post: 02-27-2009, 06:06 PM
  5. Replies: 25
    Last Post: 09-07-2005, 12:05 AM

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