Alter my VBA date code to look for month & year instead?

danbates

Active Member
Joined
Oct 8, 2017
Messages
377
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have this code that finds todays date but I was wondering if it could be altered to search and select the current month and year instead?

VBA Code:
Sub AAAAAA()

Dim C As Range
Dim Flag As Boolean
Dim D As Date

Flag = False
D = Date

    For Each c In Range("AA2:AA13")
        If C = D Then
        Flag = True
            c.Select
        End If
    Next c

If Flag = False Then
MsgBox "No Date Found"
Exit Sub
End If

End Sub

In my range I have the following and they are formatted this way as well:
Jan-21
Feb-21
Mar-21 and so on for the year.

Any help would be appreciated.

Thanks
Dan
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
There are a lot of ways of doing this. Here are 2 more options.
These rely on the months in your spreadsheet being Dates and not text.

VBA Code:
If DateSerial(Year(C), Month(C), 1) = DateSerial(Year(D), Month(D), 1) Then

VBA Code:
If Format(C, "mmm-yy") = Format(D, "mmm-yy") Then
 
Upvote 0
There is probably a simpler way that I'm missing, but keeping the basic structure of your original code.
VBA Code:
Sub AAAAAA()

Dim C As Range
Dim Flag As Boolean
Dim D As Variant
Flag = False
D = [eomonth(today(),-1)+1]

    For Each C In Range("AA2:AA13")
        If C = D Then
        Flag = True
            C.Select
        End If
    Next C

If Flag = False Then
MsgBox "No Date Found"
Exit Sub
End If

End Sub
Or, using a different method
VBA Code:
Sub BBBB()
Dim dFound As Range
Set dFound = Range("AA2:AA13").Find(Format(Date, "mmm-yy"), , xlValues, xlWhole)
If Not dFound Is Nothing Then dFound.Select Else MsgBox "Date not found"
End Sub
 
Upvote 0
Solution
Hi,
Thank you all for your suggestions.
I am going to use jasonb75's second code, just because it's a smaller code and I actually understand it.

Thanks
Dan
 
Upvote 0

Forum statistics

Threads
1,215,632
Messages
6,125,909
Members
449,274
Latest member
mrcsbenson

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