Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

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

Office Tab Enable Tabbed Editing and Browsing in Office, and Make Your Work Much Easier...
Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%
  • Reuse Anything: Add the most used or complex formulas, charts and anything else to your favorites, and quickly reuse them in the future.
  • More than 20 text features: Extract Number from Text String; Extract or Remove Part of Texts; Convert Numbers and Currencies to English Words.
  • Merge Tools: Multiple Workbooks and Sheets into One; Merge Multiple Cells/Rows/Columns Without Losing Data; Merge Duplicate Rows and Sum.
  • Split Tools: Split Data into Multiple Sheets Based on Value; One Workbook to Multiple Excel, PDF or CSV Files; One Column to Multiple Columns.
  • Paste Skipping Hidden/Filtered Rows; Count And Sum by Background Color; Send Personalized Emails to Multiple Recipients in Bulk.
  • Super Filter: Create advanced filter schemes and apply to any sheets; Sort by week, day, frequency and more; Filter by bold, formulas, comment...
  • More than 300 powerful features; Works with Office 2007-2019 and 365; Supports all languages; Easy deploying in your enterprise or organization.

arrow blue right bubble 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.


arrow blue right bubbleRelated articles:


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.
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
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Heather · 3 months ago
    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.
  • To post as a guest, your comment is unpublished.
    Dylan · 4 months ago
    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?
    • To post as a guest, your comment is unpublished.
      Heather · 3 months ago
      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.
    • To post as a guest, your comment is unpublished.
      Heather · 3 months ago
      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.
    • To post as a guest, your comment is unpublished.
      crystal · 3 months ago
      Hi Dylan,
      Sorry can't help you with that yet. Thank you for your comment.
  • To post as a guest, your comment is unpublished.
    Max · 1 years ago
    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.
  • To post as a guest, your comment is unpublished.
    Travis · 1 years ago
    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
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      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
  • To post as a guest, your comment is unpublished.
    Tee · 1 years ago
    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)
  • To post as a guest, your comment is unpublished.
    Dre · 1 years ago
    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
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Good day,
      The code works well in my case. Can you tell me your Office version?
  • To post as a guest, your comment is unpublished.
    Paul · 1 years ago
    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
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      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
  • To post as a guest, your comment is unpublished.
    Paul · 1 years ago
    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
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Sorry I get your point. (miss the above code)
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      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?
  • To post as a guest, your comment is unpublished.
    Joel · 2 years ago
    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.
    • To post as a guest, your comment is unpublished.
      Nick · 2 years ago
      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.
      • To post as a guest, your comment is unpublished.
        Attila · 2 years ago
        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
        • To post as a guest, your comment is unpublished.
          crystal · 2 years ago
          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