Macro to add date when you double click a cell in a specific range

Raceman

Board Regular
Joined
Mar 11, 2010
Messages
64
Hi, I'm trying to figure out how to write a macro that will add date to a cell if you double click it. But this feature should only apply to a range of cells $C4:$E1000. The date should not change the next day when the file is opened
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi

Add to the worksheet module:

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

' only for range C4:E1000
If Intersect(Target, Range("C4:E1000")) Is Nothing Then Exit Sub

Application.EnableEvents = False
Target.Value = Date
Application.EnableEvents = True
Cancel = True
End Sub
 
Upvote 0
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

' only for range C4:E1000
If Intersect(Target, Range("C4:E1000")) Is Nothing Then Exit Sub

[COLOR=#FF0000][B]Application.EnableEvents = False[/B][/COLOR]
Target.Value = Date
[COLOR=#FF0000][B]Application.EnableEvents = True[/B][/COLOR]
Cancel = True
End Sub
I am not sure I see any benefit in disabling events within the BeforeDoubleClick procedure. If there was code in the Change event that applied to the subject range, why wouldn't you want it to run?
 
Upvote 0
I am not sure I see any benefit in disabling events within the BeforeDoubleClick procedure. If there was code in the Change event that applied to the subject range, why wouldn't you want it to run?


Well, I don't know the OP's complete solution and so I don't know if the Change event should or should not run after the double-click.

I think that in the code I posted it's very clear that no other event is fired when the date is written in the cell. I think that's a good thing. It's very easy to delete the enable/disable statements if they are not needed. Even a less experienced user reads the code and understands it. Those statements shout it loud and clear.

The other alternative would be not to have included them in the code. In my opinion that's not so good. A less experienced user might not know that events can fire other events and it might not be so easy to understand what was happening if things didn't work as expected.

It's just my opinion, of course. I understand there would be also valid arguments against including the statements.
 
Upvote 0
Well, I don't know the OP's complete solution and so I don't know if the Change event should or should not run after the double-click.

I think that in the code I posted it's very clear that no other event is fired when the date is written in the cell. I think that's a good thing. It's very easy to delete the enable/disable statements if they are not needed. Even a less experienced user reads the code and understands it. Those statements shout it loud and clear.

The other alternative would be not to have included them in the code. In my opinion that's not so good. A less experienced user might not know that events can fire other events and it might not be so easy to understand what was happening if things didn't work as expected.

It's just my opinion, of course. I understand there would be also valid arguments against including the statements.

Fair enough.
thumbsup.gif
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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