Tomorrows Date - double click

trevolly

Board Regular
Joined
Aug 22, 2021
Messages
120
Office Version
  1. 365
Platform
  1. Windows
Hi all

Can I ask for some help on this vba code? I'm trying to set up a code for when I double click on a cell it enters tomorrows date (not tomorrow, the 16/09 but whatever the next days date is) This code brings up an error ...... "Compile Error: Ambiguous name detected: Worksheet_BeforeDoubleClick

Cheers all
T

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Date stamp
If Not Intersect(Target, Range("D20")) Is Nothing Then
Cancel = True
Target.Value = Today() + 1
End If
End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
code brings up an error ...... "Compile Error: Ambiguous name detected: Worksheet_BeforeDoubleClick
It sound like you maybe have two blocs of code with the same name "Worksheet_BeforeDoubleClick". That is not allowed. You cannot have two procedures with the same name inside the same module. You will need to combine them into one.
 
Upvote 0
try

VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Date stamp
If Not Intersect(Target, Range("D20")) Is Nothing Then
Cancel = True
Target.Value = Target.Value + 1
End If
End Sub
 
Upvote 0
It sound like you maybe have two blocs of code with the same name "Worksheet_BeforeDoubleClick". That is not allowed. You cannot have two procedures with the same name inside the same module. You will need to combine them into one.

Ahhh Ok. Yeh Ive got a vba code on the worksheet for inputting the time - 1 hour (zulu) when I double click certain cells. I was hoping to apply double click with the date + 1 day for different cells on the same worksheet. Oh well back to the drawing board

Thanks
 
Upvote 0
Ahhh Ok. Yeh Ive got a vba code on the worksheet for inputting the time - 1 hour (zulu) when I double click certain cells. I was hoping to apply double click with the date + 1 day for different cells on the same worksheet. Oh well back to the drawing board
If they are applying to different cells, then there should be no problem with combining them in one procedure!

Note the block of code that you currently have:
VBA Code:
If Not Intersect(Target, Range("D20")) Is Nothing Then
    Cancel = True
    Target.Value = Today() + 1
End If
This tells it to ONLY do this if cell D20 is double-clicked.

So you can easily add another block of code under this that affects some other cell.
You can actually add as many as you want!

So you should be able to comvine your two procedures into one.
If you have any trouble with that, please post the other procedure code.
 
Upvote 0
Ah that sounds promising... yeh its different cells that the two codes would apply to, Atm Ive got this running on the worksheet...and it works

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Date stamp
If Not Intersect(Target, Range("F2,E23,K20,K23,M20,M23")) Is Nothing Then
Cancel = True
Target.Value = Now() - (1 / 24)
End If
End Sub

I did add a line of code but it failed. Any help would be greatly appreciated
Thanks for your time
T
 
Upvote 0
Quite literally, all you have to do is take the body of the code of the one, and drop it into the other after what is already in there, i.e.
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    'Date stamp
    If Not Intersect(Target, Range("F2,E23,K20,K23,M20,M23")) Is Nothing Then
        Cancel = True
        Target.Value = Now() - (1 / 24)
    End If
    
    If Not Intersect(Target, Range("D20")) Is Nothing Then
        Cancel = True
        Target.Value = Today() + 1
    End If
    
End Sub
 
Upvote 0
Solution
Quite literally, all you have to do is take the body of the code of the one, and drop it into the other after what is already in there, i.e.
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    'Date stamp
    If Not Intersect(Target, Range("F2,E23,K20,K23,M20,M23")) Is Nothing Then
        Cancel = True
        Target.Value = Now() - (1 / 24)
    End If
   
    If Not Intersect(Target, Range("D20")) Is Nothing Then
        Cancel = True
        Target.Value = Today() + 1
    End If
   
End Sub

Lol I tried that but I didnt leave the first "End If" command in!! Thank you
 
Upvote 0
Hey @Joe4 - tried this code today and when I double clicked in one of the cells with with the +1 date (ie: E21) I got a Compile error and the first line of the code went yellow

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Date stamp with zulu
If Not Intersect(Target, Range("H24,K24,N24,D28:D52")) Is Nothing Then
Cancel = True
Target.Value = Now() - (1 / 24)
End If

If Not Intersect(Target, Range("E21,J21,N21,E24")) Is Nothing Then
Cancel = True
Target.Value = Today() + 1
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,407
Messages
6,124,723
Members
449,184
Latest member
COrmerod

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