Domingoraine
New Member
- Joined
- Nov 7, 2022
- Messages
- 14
- Office Version
- 365
- Platform
- Windows
Hello, good day. I need help to execute my VBA code. I'm practically new in using VBA.
Here's the code I copied from another forum here.
Basically, what I want to do is auto stamp the username, date and time stamp on column L once they choose either rejected or approved on the drop down menu.
At the same time, I want to lock column L as well.
Is there a way to do this? And where do I put my VBA code, on the sheet itself or create another module?
Yesterday it worked, but then when I tried to lock column L, it suddenly stopped working.
Not sure, what's the cause of it.
Please help.
Thank you in advance.
Here's a screenshot from my excel sheet,VBA, and the code I used.
Private Sub Worksheet_Change(ByVal Target As Range)
' Define object variables
Dim statusRange As Range
Dim changedCell As Range
' Define variables
Dim currentUserName As String
Dim userNameColumn As String
Dim statusColumnNumber As Integer
Dim statusValuesList As Variant
' <<< Customize this >>>
Set statusRange = Range("K4:K100") ' Limit the cells that will record the status
statusValuesList = Array("Approved", "Rejected") ' Add more status values separated by commas and inside quotes (this is not case-sensitive)
userNameColumn = "L" ' Column letter where the UserName is going to be stamped
' Prevent from firing other events while making changes to cells
Application.EnableEvents = False
' Validate if cell changed belongs to valid column and rows and if it has a valid status
If Not Intersect(Target, statusRange) Is Nothing Then
For Each changedCell In Target
If Not IsError(Application.Match(changedCell.Value, statusValuesList, 0)) Then
' Get current username
currentUserName = Environ("Username")
Else
' Empty username string
currentUserName = vbNullString
End If
' Assign username to cell in previously defined column and same row
Range(userNameColumn & changedCell.Row).Value = currentUserName
Next changedCell
End If
' Reenable firing events
Application.EnableEvents = True
End Sub
Here's the code I copied from another forum here.
Basically, what I want to do is auto stamp the username, date and time stamp on column L once they choose either rejected or approved on the drop down menu.
At the same time, I want to lock column L as well.
Is there a way to do this? And where do I put my VBA code, on the sheet itself or create another module?
Yesterday it worked, but then when I tried to lock column L, it suddenly stopped working.
Not sure, what's the cause of it.
Please help.
Thank you in advance.
Here's a screenshot from my excel sheet,VBA, and the code I used.
Private Sub Worksheet_Change(ByVal Target As Range)
' Define object variables
Dim statusRange As Range
Dim changedCell As Range
' Define variables
Dim currentUserName As String
Dim userNameColumn As String
Dim statusColumnNumber As Integer
Dim statusValuesList As Variant
' <<< Customize this >>>
Set statusRange = Range("K4:K100") ' Limit the cells that will record the status
statusValuesList = Array("Approved", "Rejected") ' Add more status values separated by commas and inside quotes (this is not case-sensitive)
userNameColumn = "L" ' Column letter where the UserName is going to be stamped
' Prevent from firing other events while making changes to cells
Application.EnableEvents = False
' Validate if cell changed belongs to valid column and rows and if it has a valid status
If Not Intersect(Target, statusRange) Is Nothing Then
For Each changedCell In Target
If Not IsError(Application.Match(changedCell.Value, statusValuesList, 0)) Then
' Get current username
currentUserName = Environ("Username")
Else
' Empty username string
currentUserName = vbNullString
End If
' Assign username to cell in previously defined column and same row
Range(userNameColumn & changedCell.Row).Value = currentUserName
Next changedCell
End If
' Reenable firing events
Application.EnableEvents = True
End Sub