Abhishek Pradhan
Abhishek Pradhan

Reputation: 25

How to add current time in an excel document for range of cell

I would like to add current system time in an excel document when i should click on the cell.

code

 If Target.Address = ActiveCell.Address Then 
        Target = Format(Now, "ttttt")    
 End If 

Upvotes: 0

Views: 42

Answers (2)

brettdj
brettdj

Reputation: 55672

You can restrict it to a range Intersect as below. When working with events you should:

  • always disable Events so that the code doesnt re-call itself (a particular issue with the Change event)
  • be clear if the Target applies to multiple cells or just a single cell.

code below uses DoubleClick event

sheet module code

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim rng1 As Range
Set rng1 = Intersect(Target, Range("A1:a10"))
If rng1 Is Nothing Then Exit Sub
Application.EnableEvents = False
rng1 = Format(Now, "ttttt")
Application.EnableEvents = True
End Sub

Upvotes: 1

chris neilsen
chris neilsen

Reputation: 53137

Assuming your code is in the SelectionChange event:

If Target.Cells.Count = 1 Then
    If Not Application.Intersect(Target, Me.Range("RangeYouWantItToWorkFor") Is Nothing Then
        '...

Upvotes: 0

Related Questions