Justin
asked on
Problems with reading dates in Autofilter in Excel using VBA
Hi, I am trying to filter between dates using a Macro and auto-filter in Excel. Yet, its reading the dates in American format, eg
, instead of filtering between 07/09/2015 and 14/09/2015, its filtering between 09/07/2015 and 09/14/2015. Can anyone fix it?
I am using this code:
Dim strdate, strdate1 As String
strdate = Sheets("Macros").Range("St artdate")
strdate1 = Sheets("Macros").Range("Fi nishdate")
Sheets("All trades").Select
Rows("1:1").Select
Selection.AutoFilter
' strdate = Application.InputBox("plea se delete all trades between which dates?")
strdate = Format(strdate, "dd/mm/yyyy")
ActiveSheet.Range("$A$1:$A F$111584") .AutoFilte r Field:=6, Criteria1:= _
">=" & strdate, Operator:=xlAnd
strdate1 = Format(strdate1, "dd/mm/yyyy")
' strdate1 = Application.InputBox("plea se delete all trades between which dates?")
ActiveSheet.Range("$A$1:$A F$111584") .AutoFilte r Field:=7, Criteria1:= _
"<=" & strdate1, Operator:=xlAnd
'
Cells.Select
Commentary-Analysis.xlsm
, instead of filtering between 07/09/2015 and 14/09/2015, its filtering between 09/07/2015 and 09/14/2015. Can anyone fix it?
I am using this code:
Dim strdate, strdate1 As String
strdate = Sheets("Macros").Range("St
strdate1 = Sheets("Macros").Range("Fi
Sheets("All trades").Select
Rows("1:1").Select
Selection.AutoFilter
' strdate = Application.InputBox("plea
strdate = Format(strdate, "dd/mm/yyyy")
ActiveSheet.Range("$A$1:$A
">=" & strdate, Operator:=xlAnd
strdate1 = Format(strdate1, "dd/mm/yyyy")
' strdate1 = Application.InputBox("plea
ActiveSheet.Range("$A$1:$A
"<=" & strdate1, Operator:=xlAnd
'
Cells.Select
Commentary-Analysis.xlsm
ASKER
It looks good, but I have a auto-filter condition on the 7th column too so that the start date is after the 7th of Sep and the finish date is before the 14th of Sep. Will this work?
Sub AutofilterAndDelete()
Dim ws As Worksheet
Dim lr As Long
Dim strdate, strdate1 As Double
'Application.ScreenUpdatin g = False
Set ws = Sheets("All trades")
lr = ws.Cells(Rows.Count, "F").End(xlUp).Row
strdate = Sheets("Macros").Range("St artdate"). Value
strdate1 = Sheets("Macros").Range("Fi nishdate") .Value
Sheets("All trades").AutoFilterMode = 0
With Sheets("All trades").Rows(1)
.AutoFilter field:=6, Criteria1:=">=" & strdate
.AutoFilter field:=7, Criteria1:="<=" & strdate1
If ws.Range("F1:F" & lr).SpecialCells(xlCellTyp eVisible). Cells.Coun t > 1 Then
ws.Range("F2:F" & lr).SpecialCells(xlCellTyp eVisible). EntireRow. Delete
End If
.AutoFilter
End With
Sub AutofilterAndDelete()
Dim ws As Worksheet
Dim lr As Long
Dim strdate, strdate1 As Double
'Application.ScreenUpdatin
Set ws = Sheets("All trades")
lr = ws.Cells(Rows.Count, "F").End(xlUp).Row
strdate = Sheets("Macros").Range("St
strdate1 = Sheets("Macros").Range("Fi
Sheets("All trades").AutoFilterMode = 0
With Sheets("All trades").Rows(1)
.AutoFilter field:=6, Criteria1:=">=" & strdate
.AutoFilter field:=7, Criteria1:="<=" & strdate1
If ws.Range("F1:F" & lr).SpecialCells(xlCellTyp
ws.Range("F2:F" & lr).SpecialCells(xlCellTyp
End If
.AutoFilter
End With
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window