logo elektroda
logo elektroda
X
logo elektroda

Excel Macro: Preventing Username and Date Appearance Before Actual Cell Modification

chris2807 5796 6
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 16229761
    chris2807
    Level 10  
    Hello,
    I am asking you for help.
    I have a macro that, when a cell in column 3, 4, or 5 is modified, inserts the modified date and username in the corresponding cell in columns A and B. The problem is that you can double-click the cell in column 3, 4, or 5 to the date and username have already appeared. How to modify a macro so that the date and username appear only after the actual modification of the cell.

    Option Explicit

    Private Sub WorkSheet_Change (ByVal Target As Range)
    Dim and As Integer

    i = Target.Row
    If Target.Column = 3 Or Target.Column = 4 Or Target.Column = 5 Then
    Range ("A" & i) .Value = Environ ("Username")
    End If

    If Target.Column = 3 Or Target.Column = 4 Or Target.Column = 5 Then
    Range ("B" & i) .Value = Date & "" & Time ()
    End If

    End Sub
  • ADVERTISEMENT
  • Helpful post
    #2 16229948
    Andie
    Level 22  
    Try this:

    Code: VBScript
    Log in, to see the code
  • ADVERTISEMENT
  • Helpful post
    #3 16230042
    JRV
    VBA, Excel specialist
    Code: text
    Log in, to see the code
  • #4 16231638
    chris2807
    Level 10  
    Thanks to everyone for your help. That was it.
  • ADVERTISEMENT
  • #5 16257176
    chris2807
    Level 10  
    I am asking you again for help. Namely, I would like the cells in columns A and B to be locked by default and only unlocked before saving the username and date, after modifying a given record.


    Option Explicit
    Private a

    Private Sub Worksheet_SelectionChange (ByVal Target As Range)

    If Not Intersect (ActiveCell, Range ("C: E")) Is Nothing Then a = ActiveCell

    End Sub
    Private Sub WorkSheet_Change (ByVal Target As Range)
    Dim and As Integer

    i = Target.Row
    If Target.Column = 3 Or Target.Column = 4 Or Target.Column = 5 Then
    If Target.Cells a Then
    Range ("A" & i) .Value = Environ ("Username")
    Range ("B" & i) .Value = Date & "" & Time ()
    End If
    End If

    End Sub
  • #6 16257969
    Andie
    Level 22  
    Enable protection on cells from columns A and B, by formatting cells -> protection -> check "Block" (in cells to be filled by users, uncheck this option). In the Ten_skoroszyt module, enter the code:
    Code: VBScript
    Log in, to see the code


    If necessary, rename the sheet and enter the security password between the quotation marks (if necessary).
  • ADVERTISEMENT
  • #7 16259383
    chris2807
    Level 10  
    Thank you for your help.

Topic summary

The discussion revolves around modifying an Excel macro to ensure that the username and date are recorded only after an actual modification of cells in columns 3, 4, or 5. Several solutions were proposed, including using a variable to store the previous value of the cell and checking it before updating the username and date. Additionally, a follow-up question addressed the need to lock columns A and B by default, with suggestions for enabling protection on these cells and using a macro to protect the worksheet upon opening. The responses provided practical code snippets to achieve these functionalities.
Summary generated by the language model.
ADVERTISEMENT