last day of the month (VBA)

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
327
Office Version
  1. 2016
Platform
  1. Windows
Hope someone can help. When I rightclick on the first 30 cells in col A (these dates are NOT the last day of the month), then a Msgbox has to appear with "NO". But when the cell IS the last day of the month, then "YES" has to appear in the Msgbox.

See the image. Thanks in advance.

 

Attachments

  • lastday.png
    lastday.png
    43.7 KB · Views: 12

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Right-click on the sheet tab name at the bottom of the screen, select "View Code", and paste this code in the VB Editor window that pops up:
VBA Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)

    If Target.CountLarge > 1 Then Exit Sub
    
'   Make sure cell right-clicked is in column A and is a date
    If Target.Column = 1 And IsDate(Target) Then
'       Check to see if it is end of month date
        If Application.WorksheetFunction.EoMonth(Target.Value, 0) = Target.Value Then
            MsgBox "Yes"
        Else
            MsgBox "No"
        End If
    End If
        
End Sub
This should do what you want.
 
Upvote 0
Solution
Might also be worth adding the line:
VBA Code:
Cancel = True
Under the line:
VBA Code:
If Target.Column = 1 And IsDate(Target) Then
This will stop the default right click menu from popping up each time.
 
Upvote 0
Might also be worth adding the line:
VBA Code:
Cancel = True
Under the line:
VBA Code:
If Target.Column = 1 And IsDate(Target) Then
This will stop the default right click menu from popping up each time.
Yeah. I am not really sure why they are wanting to use right-click instead of the normal left-click/select, unless they purposely want the menu to open.
 
Upvote 0
You are welcome.
Glad we were able to help!
 
Upvote 0

Forum statistics

Threads
1,216,590
Messages
6,131,610
Members
449,657
Latest member
Timber5

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