Code to insert current date and time when a value is enter into column B

Offroadracer_814

New Member
Joined
Aug 7, 2015
Messages
24
Hello,

Thanks for taking you time to read my problem. I am tracking my blood pressure. With Column A being current date and time (m/d/yy
h:m AM/PM).

What I envision is once I but the first number into column B. That a code would insert the current date and time. Then make my active cell as column C to enter the next number.

I found this code on the internet. But, I can not figure out the offset correctly. Plus, I know I have to change the Target Column to 2. But, it is kicking my backside.

'Code by Sumit Bansal from https://trumpexcel.com
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Handler
If Target.Column = 1 And Target.Value <> "" Then
Application.EnableEvents = False
Target.Offset(0, 1) = Format(Now(), "dd-mm-yyyy hh:mm:ss")
Application.EnableEvents = True
End If
Handler:
End Sub'Code by Sumit Bansal from https://trumpexcel.com

Any help would be appreciated.

Thanks

Kevin
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Code for insert to Column Next
Code:
[COLOR=#333333]Private Sub Worksheet_Change(ByVal Target As Range)
[/COLOR]Static i As Integer
[COLOR=#333333]On Error GoTo Handler[/COLOR]
[COLOR=#333333]If Target.Column = 1 And Target.Value <> "" Then[/COLOR]
[COLOR=#333333]Application.EnableEvents = False
[/COLOR]
For i = 1 to 200
If Target.Offset(0, i) <> "" Then Exit For
Next

[COLOR=#333333]Target.Offset(0, i) = Format(Now(), "dd-mm-yyyy hh:mm:ss")[/COLOR]
[COLOR=#333333]
Application.EnableEvents = True[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]Handler:[/COLOR]
[COLOR=#333333]End Sub[/COLOR]
 
Upvote 0
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count <> 1 Then Exit Sub
Application.EnableEvents = False
If Target.Column = 2 Then
    If Target.Value = "" Then
        Target(1, 0).ClearContents
    Else
        Target(1, 0) = Format(Now(), "dd-mm-yyyy hh:mm:ss")
        Target(1, 2).Select
    End If
ElseIf Target.Column = 3 Then Target(2, 0).Select
End If
Application.EnableEvents = True
End Sub
 
Last edited:
Upvote 0
This version will undo copy/paste to multiple col B cells at a time and clear date/time from col A if user wants to delete a prior col B entry.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Columns("B")) Is Nothing Then
    If Target.Count = 1 Then
        If Target.Value <> "" Then
            Application.EnableEvents = False
            Target.Offset(0, -1).Value = Now
            Target.Offset(0, 1).Select
            Application.EnableEvents = True
            Exit Sub
        Else
            If Not IsEmpty(Target.Offset(0, -1)) Then
                Application.EnableEvents = False
                Target.Offset(0, -1) = ""
                Target.Offset(0, 1).Select
                Application.EnableEvents = True
                Exit Sub
            End If
        End If
    End If
    MsgBox "Confine column B entries to one cell per entry"
    With Application
        .EnableEvents = False
        .Undo
        .EnableEvents = True
        Exit Sub
    End With
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top