VBA Code not Executing

Domingoraine

New Member
Joined
Nov 7, 2022
Messages
14
Office Version
  1. 365
Platform
  1. 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.

1667882548369.png


1667882635559.png




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
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
I did. I tried it but its still not working.. Im using Office 365. Yesterday it worked but now it stopped working.
You will see in the code you must enter "Approved" or Rejected for the script to run.
Change this if you want to "Alpha" Or "Bravo" whatever you want

Remove the code you already have in the sheet
 
Upvote 0
I did. I tried it but its still not working.. Im using Office 365. Yesterday it worked but now it stopped working.
Not sure what your saying now. Worked yesterday but not today.
I asked you to show me all the code you have in your sheet but you did not show me it.
 
Upvote 0
Not sure what your saying now. Worked yesterday but not today.
I asked you to show me all the code you have in your sheet but you did not show me it.
Where you see in the code Select case
The words you put in there must be exactly the same as you enter into column K
Capitulation is important.
 
Upvote 0
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  11/8/2022  2:22:18 AM  EST
If Target.Column = 11 Then

Select Case Target.Value
    Case "Approved", "Rejected"
        Target.Offset(, 1).Value = Application.UserName & "," & Now()
        
        Case Else
        MsgBox "You entered: " & Target.Value & vbNewLine & "That's not a proper value", , "Oops"
        
    End Select
End If
End Sub
 
Upvote 0
Where you see in the code Select case
The words you put in there must be exactly the same as you enter into column K
Capitulation is important.
I followed the codes and also typed it exactly the same in column K but didnt work.
My sheet is already saved as macro enabled

1667892569532.png


1667892547802.png
 
Upvote 0
So you tell me.
You said it worked yesterday but now does not work today.
I have no answer sorry

I assume you agree column K and Column 11 are the same.
I know in some other countries. This is not the same
Some countries the columns from what I understand go from Right to left
As in US columns go from Left to right
 
Upvote 0
Tried this but also didnt work. :( What format should Column L be? Whenever I typed in Approved or Rejected, nothing appears on Column L.
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  11/8/2022  2:22:18 AM  EST
If Target.Column = 11 Then

Select Case Target.Value
    Case "Approved", "Rejected"
        Target.Offset(, 1).Value = Application.UserName & "," & Now()
       
        Case Else
        MsgBox "You entered: " & Target.Value & vbNewLine & "That's not a proper value", , "Oops"
       
    End Select
End If
End Sub
 
Upvote 0
So you tell me.
You said it worked yesterday but now does not work today.
I have no answer sorry

I assume you agree column K and Column 11 are the same.
I know in some other countries. This is not the same
Some countries the columns from what I understand go from Right to left
As in US columns go from Left to right
Yes, that's right. Column K and Column 11 is the same. It's alright. thanks for trying to help my anyway. Appreciate your effort. :)
 
Upvote 0

Forum statistics

Threads
1,223,631
Messages
6,173,460
Members
452,516
Latest member
archcalx

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