Check for a specific month in a column

sofas

Well-known Member
Joined
Sep 11, 2022
Messages
559
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Hi I have several dates in column L
Day/date/month/year

Before executing a specific code, what is required is to verify that there is at least one month that corresponds to cell K2, which contains the name of the month only. If it is not found, the execution of the macro is canceled. I tried this, but it does not respond.

VBA Code:
Sub example()
Set ws = ActiveSheet
Dim StartDate As Range
Dim rgFound As Range
  lastrow = ws.Range("L" & Rows.Count).End(xlUp).Row

    Set StartDate = ws.Range("k2")
     Set rgFound = Range("L1:l" & lastrow).Find(What:=Month(StartDate), LookAt:=xlWhole, LookIn:=xlValues)
                                              

        If rgFound Is Nothing Then
            MsgBox "The date Not Found.", vbOKOnly + vbExclamation
            Exit Sub
        End If
'When checking for the same month

      'Run macro
    '  ....................................


End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Sofas,

What values do you have in the cells you are searching? Do you have dates, or text with the month names? What you are searching for is a number representing the month number. If you have dates or month names like Jan or January, the search will fail.
 
Upvote 0
This may work if the search range is full of dates

VBA Code:
Sub example()

  Dim StartDate As Range
  Dim rgFound As Range
  Dim cel As Range
  Dim Rng As Range
  
  Set ws = ActiveSheet
  
  lastRow = ws.Range("L" & Rows.Count).End(xlUp).Row

  Set StartDate = ws.Range("k2")
  Set Rng = Range("L1:l" & lastRow)
  For Each cel In Rng
    If Month(cel) = Month(StartDate) Then
      Set rgFound = cel
      Exit For
    End If
  Next cel

  If rgFound Is Nothing Then
      MsgBox "The date Not Found.", vbOKOnly + vbExclamation
      Exit Sub
  End If


End Sub
 
Upvote 0
Solution
Sofas,

What values do you have in the cells you are searching? Do you have dates, or text with the month names? What you are searching for is a number representing the month number. If you have dates or month names like Jan or January, the search will fail.
Yes, I have the dates like this, for example. The value of research is February
Monday February 26, 2024
 
Upvote 0
Please look at what is in the cell, not displayed. When you edit the cell, what shows? That my just be a number format displaying the date like that.
 
Upvote 0
This may work if the search range is full of dates

VBA Code:
Sub example()

  Dim StartDate As Range
  Dim rgFound As Range
  Dim cel As Range
  Dim Rng As Range
 
  Set ws = ActiveSheet
 
  lastRow = ws.Range("L" & Rows.Count).End(xlUp).Row

  Set StartDate = ws.Range("k2")
  Set Rng = Range("L1:l" & lastRow)
  For Each cel In Rng
    If Month(cel) = Month(StartDate) Then
      Set rgFound = cel
      Exit For
    End If
  Next cel

  If rgFound Is Nothing Then
      MsgBox "The date Not Found.", vbOKOnly + vbExclamation
      Exit Sub
  End If


End Sub
Thank you, it was successful. When editing the search cell or search column, the date is formatted in this format: 02/26/2024
In general, the code works very well for me
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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