unknownymous

Board Regular
Joined
Sep 19, 2017
Messages
249
Office Version
  1. 2016
Platform
  1. Windows
Hi Guys,

Can you possibly help me of how to code for this scenario:

Basically, I need to exclude the data not older than 4 years. However for the computation, the start date is not based from the current date but I want to set the date so a message box will pop asking me what is the starting date for computation.

CodeNameQtyDateSource
001Ben50005/01/2010Book 1
002Len15005/01/2019Book 5
003Sean10005/01/2018Book 4
004Roan35005/01/2013Book 1

<tbody>
</tbody>

Result: Exclude Data More than 4 Years

CodeNameQtyDate Source
002Len15005/01/2019Book 5
003Sean10005/01/2018Book 4
Excluded
001Ben50005/01/2010Book 1
004Roan35005/01/2013Book 1

<tbody>
</tbody>



****** id="cke_pastebin" style="position: absolute; top: 115.764px; width: 1px; height: 1px; overflow: hidden; left: -1000px;">
Book 1

<tbody>
</tbody>
</body>Any thoughts will be much appreciated.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Try:
Code:
Sub ExludeDates()
    Application.ScreenUpdating = False
    Dim LastRow As Long, sDate As String
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    sDate = InputBox("Please enter a starting date.", "Start Date", Format(Date, "yyyy/mm/dd"))
    If sDate = "" Then Exit Sub
    With Cells(1, 1).CurrentRegion
        .AutoFilter 4, "<" & DateSerial(Year(CDate(sDate)) - 4, Month(CDate(sDate)), Day(CDate(sDate)))
        Cells(Rows.Count, "A").End(xlUp).Offset(2) = "Excluded"
        Range("A2:E" & LastRow).SpecialCells(xlCellTypeVisible).Copy Cells(Rows.Count, "A").End(xlUp).Offset(1)
        Range("A2:E" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        .AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Thanks Mumps!

By the way, can you confirm if in the code "With Cells(1,1) = Row, Column? Still learning with coding.

I did ask as I try the codes where the header is in Row 3 and the date is in 5th column. When I run the macro, the header went missing and found that its in the EXCLUDED SECTION

Excel Sheet:
Row 1 = Title
Row 2 = Data Description
Row3 = HEADERS
Row 4 = DATA STARTS and so on


Can you help to modify the codes?


Thank you! :)
 
Upvote 0
Try:
Code:
Sub ExludeDates()
    Application.ScreenUpdating = False
    Dim LastRow As Long, sDate As String
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    sDate = InputBox("Please enter a starting date.", "Start Date", Format(Date, "yyyy/mm/dd"))
    If sDate = "" Then Exit Sub
    With Cells(1, 1).CurrentRegion
        .AutoFilter 4, "<" & DateSerial(Year(CDate(sDate)) - 4, Month(CDate(sDate)), Day(CDate(sDate)))
        Cells(Rows.Count, "A").End(xlUp).Offset(2) = "Excluded"
        Range("A4:E" & LastRow).SpecialCells(xlCellTypeVisible).Copy Cells(Rows.Count, "A").End(xlUp).Offset(1)
        Range("A4:E" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        .AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub

"Cells(1,1)" is the same as "Range("A1"). Either one can be used.
 
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,580
Members
448,972
Latest member
Shantanu2024

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top