Thread: [Solved] Date/Time Entry
View Single Post
 
Old 03-06-2016, 10:12 PM
Philb1 Philb1 is offline Windows 10 Office 2010 32bit
Advanced Beginner
 
Join Date: Feb 2016
Location: Auckland
Posts: 43
Philb1 is on a distinguished road
Default

Hi
Your code has separated the two column routines completely. It worked if you clicked column B, but if you clicked column D, it checked if column B was the target and if it wasn't the whole routine terminated.
Here is how I would do it
Cheers

Code:
Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  
Dim BCol As Range, DCol As Range
Set BCol = Range("B:B")
Set DCol = Range("D:D")

'   If any errors occur make sure Enable Events is on when macro exited
On Error GoTo ExitOut

If Not Intersect(Target, BCol) Is Nothing Or Not Intersect(Target, DCol) Is Nothing Then
Application.EnableEvents = False
    If Target.Row >= 2 Then ' Minimum of row 2
        With Target.Offset(, 1) ' Put date & time in adjacent column to the right
            .Value = Now
            .NumberFormat = "dd/mm/yyyy hh:mm:ss" ' Change to suit
        End With
        GoTo ExitOut
    End If
End If
GoTo ExitOut

ExitOut:
On Error GoTo 0
Application.EnableEvents = True
Exit Sub

End Sub
Reply With Quote