This may be a simple question, but how might I specify a month as criteria when performing a search in Excel? For example, I’d like to locate the first row where the date contains the month of June, how might I do this?
Thanks!
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Searching for Month (97 SR2)
Beautiful!
I suppose I can use this to return the first row where the first date is returned, how might I return the last row?
Is there a way I can define criteria in VBA to search for the first row containing a given month, where the cells contain standard dates? I figure I can then use xlPrevious and xlNext to find the first/last rows…
Thanks for all the help!
Drk, depends what your objective is. You can find months directly in date cells depending on format just by searching for “06/” if you’re using US mm/dd/yyyy date format and the date is entered directly (constant) rather than a formula.
Using the Find dialog is not going to directly give you the first and last rows. Here’s some code I cobbled together to get both first and last rows with a specified month. It’s also format dependent and probably needs work.:
Sub FindFLMonth()
Dim rngCell As Range
Dim varCellVal As Variant
Dim lngCellRow As Long, lngFrstM As Long, lngLstM As Long
Dim intMonth As Integer, intCellM As Integer
intMonth = Application.InputBox(“Enter Month: “, “Find First & Last Row containing Month”, , , , , , 1)
For Each rngCell In Selection.EntireColumn.SpecialCells(xlCellTypeConstants)
varCellVal = rngCell.Value
If TypeName(varCellVal) = “Date” Then
intCellM = Val(Left(varCellVal, InStr(varCellVal, “/”) – 1))
If intCellM = intMonth Then lngCellRow = rngCell.Row
If lngFrstM = 0 Then lngFrstM = lngCellRow
lngLstM = lngCellRow
End If
Next rngCell
MsgBox “First: ” & lngFrstM & vbLf & _
“Last: ” & lngLstM
End Sub
In order to get the last occurrence of a given month, use one of:
=INDEX(A2:A6,MATCH(E1,TEXT(A2:A6,”mmmm”))) where E1 is e.g., “June”,
=INDEX(A2:A6,MATCH(E1,MONTH(A2:A6))) where E1 is e.g., 6.
Both are again array-entered (using control+shift+enter).
I must leave the VBA part of your question to someone else.
Aladin
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications