Need VBA Assistance - Filter column if cells contain current month / year

sparkytech

Board Regular
Joined
Mar 6, 2018
Messages
96
Office Version
  1. 365
  2. 2019
I have column "W" with text comments that also contain dates. For example: "abc ~ 06.01.22 ~ This is a sample comment!"

I need help creating a filter to look at column "W", and only show rows that contain the current month / year in this string of text in cell "W'. My thought is that it could look for the string: {current month number}.**.{current year}. I hope that makes sense... is this possible?
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Try this:

VBA Code:
Sub Macro1()
  If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  Range("A1", Range("W" & Rows.Count).End(3)).AutoFilter Field:=23, Criteria1:="*" & Format(Date, "mm") & ".*" & Format(Date, "yy") & "*"
End Sub
 
Upvote 0
Try this:

VBA Code:
Sub Macro1()
  If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  Range("A1", Range("W" & Rows.Count).End(3)).AutoFilter Field:=23, Criteria1:="*" & Format(Date, "mm") & ".*" & Format(Date, "yy") & "*"
End Sub
Thanks! This works, but needs a tweak. When I run this, it does correctly show a row with 06.01.22 in the text string in column "W", but also shows a row with 04.06.22. (which needs to be filtered out, as 04 is not the current month). I think the filter sees the 06.22 and decides that is current month, even though its from April. I'm still learning VBA... does this (or can it) look at only the first 2 digits as month, and the last 2 digits as year, and ignore the middle 2 digits? Also if its not too much to ask, can it filter/show current and prior month? Thanks again!
 
Upvote 0
Also if its not too much to ask, can it filter/show current and prior month?
Try this:

VBA Code:
Sub Macro1()
  If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  Range("A1", Range("W" & Rows.Count).End(3)).AutoFilter Field:=23, _
    Criteria1:="*" & Format(Date, "mm") & ".*." & Format(Date, "yy") & "*", _
    Operator:=xlOr, _
    Criteria2:="*" & Format(DateSerial(Year(Date), Month(Date) - 1, 1), "mm") & ".*." & Format(Date, "yy") & "*"
End Sub
 
Upvote 0
Solution
Try this:

VBA Code:
Sub Macro1()
  If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  Range("A1", Range("W" & Rows.Count).End(3)).AutoFilter Field:=23, _
    Criteria1:="*" & Format(Date, "mm") & ".*." & Format(Date, "yy") & "*", _
    Operator:=xlOr, _
    Criteria2:="*" & Format(DateSerial(Year(Date), Month(Date) - 1, 1), "mm") & ".*." & Format(Date, "yy") & "*"
End Sub
That works perfect, thanks!
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,383
Members
448,955
Latest member
BatCoder

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