VBA for Date

M_S

New Member
Joined
Aug 6, 2021
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a spreadsheet wherein dates are stored as Text, I want to convert them to date format mm/dd/yyyy and then filter dates to NOT include current month. So, for example I don't want anything from Sept to show up, I only want prior months. Please note, I have other data on the left & right of date column. I want to do this using VBA.

Any help would be much appreciated. Thanks!


Date
2021-08-25
2021-08-25
2021-08-25
2021-01-14
2021-05-31
2021-05-31
2021-09-03
2021-09-03
2021-09-03
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
VBA Code:
Sub ConvertToDateAndFilter()
    Dim A, i As Long
    With Range("A2:A10")
        .NumberFormat = "mm/dd/yyyy"
        A = .Value
        For i = 1 To UBound(A)
            A(i, 1) = DateValue(A(i, 1))
        Next
        .Value = A
        .AutoFilter Field:=1, Operator:=xlAnd, Criteria1:="<=" & Date - Day(DateValue(Date))
    End With
End Sub

201914.xlsx
A
1Date
22021-08-25
32021-08-25
42021-08-25
52021-01-14
62021-05-31
72021-05-31
82021-09-03
92021-09-03
102021-09-03
Sheet3


201914.xlsx
A
1Date
208/25/2021
308/25/2021
408/25/2021
501/14/2021
605/31/2021
705/31/2021
Sheet3
 
Upvote 0
Solution
Lots of things I should be doing besides trying to learn Excel vba (much better at Access vba). I was so close yet so far on the filtering part! I didn't know about the "operator" part and the filtering wasn't right even after more than an hour (I had """") at the very end. M$ documentation is such that if you don't really know the model, it's hard to find anything. Thanks for the lesson. My code finally works, but I suppose OP will need a dynamic range?

VBA Code:
Dim theDate As Date
Dim mth As Integer, yr As Integer

mth = Month(Date)
yr = Year(Date)

theDate = DateSerial(yr, mth, 1)

    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$A$9").AutoFilter Field:=1, Operator:=xlAnd, Criteria1:="<=" & theDate
 
Upvote 0
Modified to have range specified from inputbox:

VBA Code:
Sub ConvertToDateAndFilter()
    Dim A, i As Long, Rng As Range
    On Error GoTo ExitSub 'if user cancels out of input box
        Set Rng = Application.InputBox("Select Range", Default:=Selection.Address, Type:=8)
    On Error GoTo 0
    With Rng
        .NumberFormat = "mm/dd/yyyy"
        A = .Value
        On Error Resume Next 'if selection includes data that can't convert to date
            For i = 1 To UBound(A)
                A(i, 1) = DateValue(A(i, 1))
            Next
        On Error GoTo 0
        .Value = A
        .AutoFilter Field:=1, Operator:=xlAnd, Criteria1:="<=" & Date - Day((Date))
    End With
ExitSub:
End Sub
 
Upvote 0
VBA Code:
Sub ConvertToDateAndFilter()
    Dim A, i As Long
    With Range("A2:A10")
        .NumberFormat = "mm/dd/yyyy"
        A = .Value
        For i = 1 To UBound(A)
            A(i, 1) = DateValue(A(i, 1))
        Next
        .Value = A
        .AutoFilter Field:=1, Operator:=xlAnd, Criteria1:="<=" & Date - Day(DateValue(Date))
    End With
End Sub
Because of the way the text dates are written, you can write your code more compactly this way...
VBA Code:
Sub ConvertToDateAndFilter()
  With Range("A2:A10")
    .TextToColumns
    .NumberFormat = "mm/dd/yyyy"
    .AutoFilter Field:=1, Operator:=xlAnd, Criteria1:="<=" & Date - Day(DateValue(Date))
  End With
End Sub
 
Upvote 0
Because of the way the text dates are written, you can write your code more compactly this way...
VBA Code:
Sub ConvertToDateAndFilter()
  With Range("A2:A10")
    .TextToColumns
    .NumberFormat = "mm/dd/yyyy"
    .AutoFilter Field:=1, Operator:=xlAnd, Criteria1:="<=" & Date - Day(DateValue(Date))
  End With
End Sub

Thank you for this, it is converting the date from text to correct format. However, it's not filtering the previous months. 
[/QUOTE]
 
Upvote 0
VBA Code:
Sub ConvertToDateAndFilter()
    Dim A, i As Long
    With Range("A2:A10")
        .NumberFormat = "mm/dd/yyyy"
        A = .Value
        For i = 1 To UBound(A)
            A(i, 1) = DateValue(A(i, 1))
        Next
        .Value = A
        .AutoFilter Field:=1, Operator:=xlAnd, Criteria1:="<=" & Date - Day(DateValue(Date))
    End With
End Sub

201914.xlsx
A
1Date
22021-08-25
32021-08-25
42021-08-25
52021-01-14
62021-05-31
72021-05-31
82021-09-03
92021-09-03
102021-09-03
Sheet3


201914.xlsx
A
1Date
208/25/2021
308/25/2021
408/25/2021
501/14/2021
605/31/2021
705/31/2021
Sheet3

Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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