Date Filter Before Date/Time Value in Cell

FrenchCelt

Board Regular
Joined
May 22, 2018
Messages
220
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm trying to write some VBA code that filters Column M for date/time values before the date/time value in cell U1. Does anyone have any suggestions?
 
What do you need VBA to do it, instead of just use the old fashioned Filters, or the new Filter function available in newer versions of Excel (included in 365)?
 
Upvote 0
It's part of a larger macro that takes a raw report and refines it into something more specific for others to use.
 
Upvote 0
You can still use VBA to apply those tools/functions.

Your question is extremely general/generic.
Exactly what is giving you trouble?
 
Upvote 0
I can't figure out the code to make it work. Column M has rows of dates/times and I want to filter them down to only show dates/times before the date/time displayed in cell U1. When I record it to see the code, it's too specific to that given date and time in that cell, but it's going to be different every time.
 
Upvote 0
What I would recommend in these cases is to turn on your Macro Recorder, and record yourself doing it manually once, then stopping the Macro Recorder.
Then view your code. You can usually use this as the basis of the VBA code you need, you usually just then need to generalize it at that point.

Here is a link that shows you how to you use the FILTER function, if you are not familiar with it.
 
Upvote 0
If you need helping generalizing it after recording, let us know.
 
Upvote 0
When I record it to see the code, it's too specific to that given date and time in that cell, but it's going to be different every time.
Without knowing anything else about your sheet name/range/layout or if you have any other filters applying when the code runs, try as a basic filter...

VBA Code:
Sub FilterDateTime()
    Application.ScreenUpdating = False
    Range("M1:M" & Range("M" & Rows.Count).End(xlUp).Row).AutoFilter 1, "<" & CDbl(Range("U1").Value)
End Sub
 
Upvote 0

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