Problem with username and time stamp

RandyD123

Active Member
Joined
Dec 4, 2013
Messages
296
Office Version
  1. 2016
Platform
  1. Windows
So I'm using this code to put a username and time stamp when a user starts an entry in Column B. The code works perfectly, until the user decides that they don't really want to make an entry and they backspace it out. The username and timestamp remain. The other problem is that if a user deletes the row the code breaks. What I need to be able to do is if the user decides that they made a mistake on that line and either delete it or back space it out the username and time stamp go away as well. The shaded area will be locked so the user can't backspace that out.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("B4:B2000")) Is Nothing Then Exit Sub

Application.EnableEvents = False

With Target
    .Offset(, 6).Value = Environ("username")
    .Offset(, 7).Value = Date
    
End With
Application.EnableEvents = True
End Sub


Any help would be most appreciated.


Book1 - Copy.xlsm
BCDEFGHI
1
2
3STW (Person Working)STO (Person Not Working)Date Shift Trade Will OccurTime StartTime EndTotal Hours Traded OffPerson Who Made This EntryDate Person Made This Entry
4Name #08Name #0301/17/23040008004.0
5Name #03Name #0102/27/23120016304.5
6Name #04Name #0303/15/23130018305.5
7Name #06Name #0104/22/23043007303.0
8Name #02Name #0404/28/230730200012.5
9Name #08Name #0405/02/23113017306.0
10Name #05Name #0205/14/23033012008.5
11Name #01Name #0306/12/23180020002.0
12Name #07Name #0107/04/23170024007.0
13Name #71Name #0207/30/23033007304.0
14Name #08Name #0208/02/23033012008.5
15Name #110Name #7208/17/23130018005.0
16Name #119Name #11808/18/23130018005.0
17Name #06Name #0408/19/23033012008.5
18Name #22Name #0109/22/23080012004.0
19Name #114Name #10509/27/23120018006.0
20Name #1170.0Randy5/8/2023
210.0
220.0
230.0
Shift Trades
Cell Formulas
RangeFormula
G4:G23G4=(TEXT(F4,"00\:00")-TEXT(E4,"00\:00"))*24
Cells with Data Validation
CellAllowCriteria
B4:C23List=Data!$B$4:$B$149
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi @RandyD123 . Thaks for posting on MrExcel board.


The other problem is that if a user deletes the row the code breaks.
I present you 2 options:

1) If they delete the whole row, the code is not executed and we allow to delete the whole row.
Try this:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Intersect(Target, Range("B4:B2000")) Is Nothing Then Exit Sub
  If Target.Count > 1 Then Exit Sub
 
  Application.EnableEvents = False
 
  With Target
    If .Value = "" Then
      .Offset(, 6).Value = ""
      .Offset(, 7).Value = ""
    Else
      .Offset(, 6).Value = Environ("username")
      .Offset(, 7).Value = Date
    End If
  End With
  Application.EnableEvents = True
End Sub


2) But if you don't want them to modify more than one cell at a time, then we send a message, undo what they did and allow only one cell to be modified:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Intersect(Target, Range("B4:B2000")) Is Nothing Then Exit Sub
 
  Application.EnableEvents = False
 
  If Target.Count > 1 Then
    MsgBox "It is not possible to modify more than one cell", vbCritical
    Application.Undo
    Application.EnableEvents = True
    Exit Sub
  End If
 
  With Target
    If .Value = "" Then
      .Offset(, 6).Value = ""
      .Offset(, 7).Value = ""
    Else
      .Offset(, 6).Value = Environ("username")
      .Offset(, 7).Value = Date
    End If
  End With
  Application.EnableEvents = True
End Sub

Make your tests and apply the one you like the most. The 2 proposals include deletion of the user and date stamp in case the cell in column B is deleted.

Or if you want another alternative, come here comment how you would like it.


--------------
I hope to hear from you soon.
Respectfully
Dante Amor
--------------
 
Upvote 0
Solution
Wasn't really sure of what you meant on the second code. But a quick test on the first code seems to work perfectly. I can delete rows, insert rows and backspace out a row, with no errors!!! Thank You!!!
 
Upvote 1

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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