ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Worksheet Functions (https://www.excelbanter.com/excel-worksheet-functions/)
-   -   Date and Time (https://www.excelbanter.com/excel-worksheet-functions/253190-date-time.html)

Max

Date and Time
 
I have this code so that when I write a name in column A, the date and time
appears both in column B. All I want is that the date appears in column B and
the time in column C. Below is the code.

Thanks in advance.

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

Dim myRow As Long

If Target.Cells.Count 1 Then
Exit Sub 'one cell a time
End If

If Intersect(Target, Me.Range("a:a")) Is Nothing Then
Exit Sub 'Look in column A only
End If

myRow = Target.Row

On Error GoTo ErrHandler:
With Me.Cells(myRow, "b")
If IsEmpty(.Value) Then
'ok to add, the cell is empty
Application.EnableEvents = False
.NumberFormat = "dd/mm/yyyy hh:mm:ss"
.Value = Now
End If
End With

ErrHandler:
Application.EnableEvents = True

End Sub

FSt1

Date and Time
 
hi
i edited the code some. tested and works in 03.
Private Sub Worksheet_Change(ByVal Target As Range)

Dim myRow As Long

If Target.Cells.Count 1 Then
Exit Sub 'one cell a time
End If

If Intersect(Target, Me.Range("a:a")) Is Nothing Then
Exit Sub 'Look in column A only
End If

myRow = Target.Row

On Error GoTo ErrHandler:
With Me.Cells(myRow, "b")
If IsEmpty(.Value) Then
'ok to add, the cell is empty
Application.EnableEvents = False
.NumberFormat = "dd/mm/yyyy" ' hh:mm:ss"
.Value = Date
End If
End With
With Me.Cells(myRow, "c")
.NumberFormat = "hh:mm:ss"
.Value = Time
End With

ErrHandler:
Application.EnableEvents = True

End Sub

regards
FSt1


"MAX" wrote:

I have this code so that when I write a name in column A, the date and time
appears both in column B. All I want is that the date appears in column B and
the time in column C. Below is the code.

Thanks in advance.

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

Dim myRow As Long

If Target.Cells.Count 1 Then
Exit Sub 'one cell a time
End If

If Intersect(Target, Me.Range("a:a")) Is Nothing Then
Exit Sub 'Look in column A only
End If

myRow = Target.Row

On Error GoTo ErrHandler:
With Me.Cells(myRow, "b")
If IsEmpty(.Value) Then
'ok to add, the cell is empty
Application.EnableEvents = False
.NumberFormat = "dd/mm/yyyy hh:mm:ss"
.Value = Now
End If
End With

ErrHandler:
Application.EnableEvents = True

End Sub



All times are GMT +1. The time now is 10:34 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com