VBA Date Filter between two dates not working

alantse2010

New Member
Joined
Jun 9, 2018
Messages
34
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
Hello, i want to filter the data between Date - 3 to Date +3, i am trying to format the date in code but not working, it return no result.

Here is my expected result:
https://imgur.com/a/83jTSTX
Please help me.
Thank you very much.


Code:
Private Sub CommandButton17_Click()
Dim today, startdate, enddate As Date
Dim startdate2, enddate2 As String
today = Date
startdate = Date - 3
enddate = Date + 3
startdate2 = Format(Date - 3, "dd/mm/yyyy")
enddate2 = Format(Date + 3, "dd/mm/yyyy")
Application.ScreenUpdating = False
Set wSheetStart = ThisWorkbook.Sheets("A2")
 wSheetStart.Activate
 wSheetStart.AutoFilterMode = False
ActiveSheet.Range("A6:AC6").AutoFilter Field:=1, Criteria1:=">=" & startdate2, Operator:=xlAnd, Criteria2:="<=" & enddate2
 ActiveSheet.Range("A777").Select
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
If the column you want to filter contains real dates (numbers), try using startdate and enddate, rather than startdate2 and enddate2 (which are strings not numbers), as your filter criteria.
 
Upvote 0
Thank you for your reply, Joe, the type of the cell is date, so that using startdate and enddate is worked in my computer, but not worked in other computer that the filter cannot filter out the date that i need to filter manually.
Is this the excel problem or date format problem?
 
Upvote 0
Don't know for sure. On the other computer, select any empty cell and enter this formula:

=ISNUMBER(A1)

change A1 to a cell having a date that doesn't filter. What value does the formula return?
 
Upvote 0
The formula return FALSE if i type =ISNUMBER(A1) on any empty cell
 
Upvote 0
The formula return FALSE if i type =ISNUMBER(A1) on any empty cell
Does A1 contain a date that doesn't filter? If yes, then the dates are text and you need to use startdate2 and enddate2. If no, then use the address of a cell with a date that doesn't filter instead of A1.
 
Upvote 0
Does A1 contain a date that doesn't filter? If yes, then the dates are text and you need to use startdate2 and enddate2. If no, then use the address of a cell with a date that doesn't filter instead of A1.
Sorry for reply not clearly.
A1 do not contain a date that doesn't filter .The filter i set at A6:AC6, the type of the A6:AC6 is date so that i use the startdate and enddate(work in my computer).
When i run the VBA on the other computer, it can filter but no result return.
The result like this:
https://imgur.com/a/IoEJhJP
 
Upvote 0
What exactly is the result you want returned? You haven't shown enough of your code or data layout to know. Are you filtering columns A:AC or what?
 
Upvote 0
Joe, the result i want to return is date, now that the code is worked, thank you very much for your help.
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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