Note: The other languages of the website are Google-translated. Back to English
English English

How to auto add/enter current date/time in a cell with double clicking in Excel?

If you need to insert current date or date time frequently in a worksheet, you can try the method in this article. This article will help you to automatically add or enter current date or date tine in a specified range cells with only double clicking.

Double click to auto add/enter current date or date time with VBA code


Double click to auto add/enter current date or date time with VBA code


You can run the below VBA code to automatically add current date or date time in a cell with double clicking. Please do as follows.

1. Right click the Sheet tab which you want to insert current date into the specified cells, then select View Code from the right-clicking menu.

2. In the Microsoft Visual basic for Applications window, copy and paste the below VBA code into the Code window.

VBA code: Double click to add current date into a cell

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Intersect(Target, Range("A1:B10")) Is Nothing Then
        Cancel = True
        Target.Formula = Date
    End If
End Sub

Notes:

1. In the code, A1:B10 is the range you will add current date to.
2. If you need to add current date time into the cell, please replace Date with Now() in the code. You can change them as you need.

3. Press Alt + Q keys simultaneously to close the Microsoft Visual Basic for Applications window and return to the worksheet.

From now on, when double clicking any cell in specified range A1:B10. The current date or date time will be entered automatically.


Related articles:


The Best Office Productivity Tools

Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial. 60-day money back guarantee.
kte tab 201905

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
officetab bottom
Comments (28)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
How do we extend this to add more cell range? I added a these extra cell ranged to the code : (Target, Range("C10:C19", "D10:D19", "E10:E19")) however it is giving me a compile error saying "wrong number of arguments or invalid property assignments" and then it highlights the first line of code you supplied "Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)" Please assist me.
This comment was minimized by the moderator on the site
Joel, don't know if you are still looking for a solution, but you need to change your code:

From: (Target, Range("C10:C19", "D10:D19", "E10:E19"))
To: (Target, Range("C10:C19,D10:D19,E10:E19"))

This will do it for you.
This comment was minimized by the moderator on the site
Hello Nick,
I'd like to get some advise from you on this subject....
I have a file that I call "productivity sheet".... On this sheet I'd like to insert the actual time,in selected cells, when the cell is clicked... (If possible, I'd like these cells after the time appears to become unchangeable .....something like to be locked.)
I do appreciate your time and thanx in advance
Attila, Hungary
exyzee@gmail.com
This comment was minimized by the moderator on the site
Dear Attila,
Please try the below screenshot to insert the actual time to cell in a certian range when it is clicked.
(The automatically locking cells function can't be acheived, sorry about that)

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False
If Not Intersect(Target, Range("A1:B10")) Is Nothing Then
Cancel = True
Target.Formula = Date
End If
End Sub
This comment was minimized by the moderator on the site
The code really works... Thank you...i have added another code to protect the cell after the entry of data. Now the problem is that, once i enter the data and the cell is protected and by mistake if I double click the protected cell, then the above code goes wrong for the whole sheet. It does not work then. I have to unprotect the sheet to bring the code live. Any solution?

The protection Code used is below:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim xRg As Range
On Error Resume Next
Set xRg = Intersect(Range("A1:a1000,b1:b1000,G1:G1000"), Target)
If xRg Is Nothing Then Exit Sub
Target.Worksheet.Unprotect Password:="123"
xRg.Locked = True
Target.Worksheet.Protect Password:="123"
End Sub
This comment was minimized by the moderator on the site
Dear Paul,
I try the code you provided. The entire worksheet will be protected immediately once I enter data into any one of the specified protected cells.
Besides, when double click on the protected cell, nothing changes to the code in my case.
Would you explain what you are exactly trying to do with the code?
This comment was minimized by the moderator on the site
Sorry I get your point. (miss the above code)
This comment was minimized by the moderator on the site
The double click entery code created is:


Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("A1:a1000")) Is Nothing Then
Cancel = True
Target.Formula = Date
End If

If Not Intersect(Target, Range("b1:b1000")) Is Nothing Then
Cancel = True
Target.Formula = Time
End If

If Not Intersect(Target, Range("g1:g1000")) Is Nothing Then
Cancel = True
Target.Formula = Time
End If
End Sub
This comment was minimized by the moderator on the site
Dear Paul,
Please try the following VBA code.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim xRg As Range
On Error Resume Next
Set xRg = Intersect(Range("A1:a1000,b1:b1000,G1:G1000"), Target)
If xRg Is Nothing Then Exit Sub
Target.Worksheet.Unprotect Password:="123"
xRg.Locked = True
Target.Worksheet.Protect Password:="123"
End Sub

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Application.EnableEvents = False
ActiveSheet.Unprotect Password:="123"
If Not Intersect(Target, Range("A1:a1000")) Is Nothing Then
Cancel = True
Target.Formula = Date
End If
If Not Intersect(Target, Range("b1:b1000")) Is Nothing Then
Cancel = True
Target.Formula = Time
End If
If Not Intersect(Target, Range("g1:g1000")) Is Nothing Then
Cancel = True
Target.Formula = Time
End If
ActiveSheet.Protect Password:="123"
Application.EnableEvents = True
End Sub
This comment was minimized by the moderator on the site
I copied and pasted the code updating the range and it did not work :-(


Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("B4:B100")) Is Nothing Then
Cancel = True
Target.Formula = Now()
End If
End Sub
This comment was minimized by the moderator on the site
Good day,
The code works well in my case. Can you tell me your Office version?
This comment was minimized by the moderator on the site
Hello, the code you gave works great. I am just curious if there is any way to have the text "double click to add date" In the cell until the date is entered. Thank you in advance ( I am trying to make my document as user friendly as possible as to not confuse my co-workers)
This comment was minimized by the moderator on the site
Hello, the above code worked great for me. Now i am just wondering if there is a way to have the text "Double click to enter date" appear in the cell until the date is entered. My goal is to make the document be as user friendly as possible. Thank you in advance
This comment was minimized by the moderator on the site
Hi Travis,
We can’t modify the code to have text display in the cell directly. But alternatively, the below optimized code will help to display the text in the cell comment, and the comment will be removed automatically after double click the cell to enter date.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A1:B10")) Is Nothing Then
Target.NoteText "double click to add date"
End If
End Sub

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("A1:B10")) Is Nothing Then
Cancel = True
Target.Comment.Delete
Target.Formula = Date
End If
End Sub
This comment was minimized by the moderator on the site
Hi there,

I copied and pasted the above code exactly as it is written into a blank workbook, however, it does not work for me. I looked at different sources on the web and most sites have a similar format as what is written above. I think perhaps there is something wrong with my VBA or some settings are not turned on. Any advice would be much appreciated. I am running Excel for Office 365 MSO (16.0.11001.20097) 32-bit on Windows 10.
This comment was minimized by the moderator on the site
Love the code and it works great. How can I make it so when I double click to execute the code its shows time in military time?
This comment was minimized by the moderator on the site
Hi Dylan,
Sorry can't help you with that yet. Thank you for your comment.
This comment was minimized by the moderator on the site
I think if you select the military time format for that cell from the Format -> Number -> Time options in your sheet that ought to work. For example, it gives the option of 1:30 PM or 13:30, so you would just select 13:30 and that should do it.
This comment was minimized by the moderator on the site
I would think that if you just select the Military Time format for that cell from the Number -> Time format options that should do it. For example, you would select 13:30 instead of 1:30 PM, and then it should display in military time.
This comment was minimized by the moderator on the site
Does anyone know if there is a way to insert this code into Excel Online? I had used it with the desktop version and it worked great but now we have migrated everything to the online platform and my date and time stamps on double click have disappeared and I can't figure out how to view or edit the code. Thanks.
This comment was minimized by the moderator on the site
So I inserted code and it works great on several sheets in my workbook, however on some sheets it just suddenly stops working after a certain row even though I have the correct range entered. Any thoughts on why this might happen.
This comment was minimized by the moderator on the site
This was just what I was looking for - this save a ton of time and I appreciate the well written instructions. Thank you!
This comment was minimized by the moderator on the site
Hello there, the code did a lot for me, How can I restrict the code to work only if field is blank. If a date is already there in the cell, double click should do nothing, regards
This comment was minimized by the moderator on the site
Hi Ahmad,
Sorry for the trouble. To only fill in the blank cells with dates with double-clicking, you can apply the following VBA code to get it done.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Updated by Extendoffice 20220609
    If Not Intersect(Target, Range("B1:C20")) Is Nothing Then
        If Target.Value = "" Then
            Cancel = True
            Target.Formula = Date
        End If
    End If
End Sub
This comment was minimized by the moderator on the site
This function did not work. Double Clicking simply enters manual edit of cell.
This comment was minimized by the moderator on the site
Hi Bob,
The code works well in my case. I need to know more specific about your issue, such as your Excel version.
And the code only works on the cells you specified.
This comment was minimized by the moderator on the site
Hi all,

I try to use that macro to use the date stamp double clicking on column E and it's working but when I try to replicate the macro to do the same but for the current time on column F it is not working as you can see attached I have an error message stating : Ambiguous Name Detected.
When I try to change the Sub WorkSheet part for another name and double click in the cells nothing happens.

Could someone help me on that ?

My code :


Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("E1:E10000")) Is Nothing Then
Cancel = True
Target.Formula = Date
End If
End Sub

Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("F1:F10000")) Is Nothing Then
Cancel = True
Target.Formula = Now()
End If
End Sub
This comment was minimized by the moderator on the site
Hi Louis,
Replicate the macro will cause two same procedures with the same name in a single sheet code window. Excel doesn't allow two or more same names of functions in a module. Not even in Events. It leads to ambiguity.
If you want to do a different task on the same event, you need to modify the original code to meet your needs.
The following VBA code can do you a favor. Please give it a try.
Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Updated by Extendoffice 20221025
    If Not Intersect(Target, Range("E1:E10000")) Is Nothing Then
        Cancel = True
        Target.Formula = Date
    End If
    If Not Intersect(Target, Range("F1:F10000")) Is Nothing Then
        Cancel = True
        Target.Formula = Date
    End If
End Sub
There are no comments posted here yet
Leave your comments
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations