VBA to compare months in dates

EchoD

New Member
Joined
Mar 4, 2022
Messages
3
Office Version
  1. 2007
I'm trying to make a macro that checks every cell with data in row A, compares it to cell A1, and highlights it if their month doesn't match.
Issue is, the dates in these cells all follow the DD/MM/YYYY format. 01/08/2021, for example. And I don't know if the Date calls work with them, or how to approach this in the first place.

I've checked every thread I could, made a junction of everything useful I've found, and I've gotten this far:

VBA Code:
Sub Test()

     Dim AMonth As Integer
     AMonth = Month(Cells(2, 1).Value)
     Dim lRow As Long, Cel As Range, Data As Range, Brange As Range, BMonth As Integer
     BMonth = ?
     With ActiveSheet
        lRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set Data = .Range("A1:A2" & lRow)
        For Each Cel In Data
                If Not BMonth = AMonth Then
                    If Not Brange Is Nothing Then Set Brange = Union(Brange, Cel) Else Set Brange = Cel
                End If
        Next Cel
        Data.Interior.Color = xlNone
        If Not Brange Is Nothing Then Brange.Interior.Color = RGB(255, 192, 0)
     End With

End Sub

Any help is appreciated!
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
So these cells are real date (aka numeric values) or just text ?​
I don't know how to answer that. They're formatted under the Date category, but they're just dd-mm-yyyy written in a field. They're supposed to be real dates, yeah.
It might not change anything, but I forgot to mention the dates aren't separated by slashes, but lines. dd-mm-yyyy
 
Upvote 0
If the cells are dates rather than texts so you can use the Month function whatever in a cell formula or under VBA …​
 
Upvote 0

For further help attach a worksheet via this forum tool XL2BB or link your workbook on a files host website like Dropbox …​
 
Upvote 0
If the cells are dates rather than texts so you can use the Month function whatever in a cell formula or under VBA …​
It should work, but I've tried a million combinations and nothing's working.
I've attached a picture of my sheet. In this case, running the macro should highlight A3 and A5, but because they're different months, not days or years. I genuinely can't wrap my head around what I might be doing wrong.
 

Attachments

  • Captura.PNG
    Captura.PNG
    18.6 KB · Views: 18
Upvote 0
Use Month VBA function for each date in order to compare them …​
 
Upvote 0
Another way without any VBA procedure : via the Excel conditional formatting with an easy formula …​
 
Upvote 0

Forum statistics

Threads
1,214,540
Messages
6,120,107
Members
448,945
Latest member
Vmanchoppy

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