How to identify if a column contains the current month and year using VBA?

Indominus

Board Regular
Joined
Jul 11, 2020
Messages
160
Office Version
  1. 2016
Platform
  1. Windows
Hello. I am trying to use VBA to identify if column A contains the current month and year then do the rest of my code versus do nothing. Thee dates were put in via VBA so the day on them are the 1st. If that matters. So I want Excel to indenting right now if column A contains any cell with May-21 (full is 5/1/2021). The format of the dates in column A are custom format “mmm-yy”. Here is the code I have so far however, it does not recognize it. Thanks

VBA Code:
Sub LocateDate()



Dim rng1 As Range



Dim strSearch As String

strSearch = Format(Date, "mmm yyyy")

Set rng1 = Range("A:A").Find(strSearch, , xlValues, xlWhole)

If Not rng1 Is Nothing Then

MsgBox ("Contains date")

Else

MsgBox ("Does not contain date")

End If



End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Maybe this works:
VBA Code:
Dim strSearch As Date
strSearch = DateSerial(Year(Date), Month(Date), 1)
Set rng1 = Range("A:A").Find(CDate(strSearch), , xlFormulas, xlWhole)
 
Upvote 0
Solution
Maybe this works:
VBA Code:
Dim strSearch As Date
strSearch = DateSerial(Year(Date), Month(Date), 1)
Set rng1 = Range("A:A").Find(CDate(strSearch), , xlFormulas, xlWhole)
Thank you this works. One more thing I just noticed if you do not mind. How would I identify if column A has the previous month and year in it. So April-21? Putting a “-1” after the Date is not working. Thanks
 
Upvote 0
Thank you this works. One more thing I just noticed if you do not mind. How would I identify if column A has the previous month and year in it. So April-21? Putting a “-1” after the Date is not working. Thanks
Never mind I figured it out. Thank you so much Mart37! I really appreciate it.
 
Upvote 0

Forum statistics

Threads
1,215,436
Messages
6,124,867
Members
449,192
Latest member
MoonDancer

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