How to activate the below both codes in the same sheet

Tariq384

New Member
Joined
Jul 23, 2020
Messages
10
Office Version
  1. 2013
Platform
  1. Windows
Dear All,

Please advise me how to activate the below (1 & 2) Codes to be functioning on the same sheet and your help is highly appreciated.

1) To show the user name on specific cell based on another cell value
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Column = 1 Or Target.Column = 2 Then
ThisRow = Target.Row
If (ThisRow = 1) Then Exit Sub
' time stamp corresponding to cell's last update
Range("D" & ThisRow).Value = Now
' Windows level UserName | Application level UserName
Range("C" & ThisRow).Value = Environ("username") & "|" & Application.UserName
Range("C:D").EntireColumn.AutoFit
End If
End Sub


2) To Lock the cell after editing
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel As Range
ActiveSheet.Unprotect Password:="01234"
For Each cel In Target
If cel.Value <> "" Then
cel.Locked = True
End If
Next cel
ActiveSheet.Protect Password:="01234"
Exit Sub

End Sub


I Like to activate them on the same worksheet.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Please remember to use the correct tags when posting code. The easy way is to click the </> icon and post your code into the pop up window (only your code goes onto that window, not your actual post or any additional text).

See if this does what you need
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel As Range
With Target
    If .Column = 1 Or .Column = 2 Then
        If .Row > 1 Then
    ' time stamp corresponding to cell's last update
        Cells(.Row, 4).Value = Now
    ' Windows level UserName | Application level UserName
        Cells(.Row, 3).Value = Environ("username") & "|" & Application.UserName
        Range("C:D").EntireColumn.AutoFit
    End If
    ActiveSheet.Unprotect Password:="01234"
        For Each cel In .Cells
            .Locked = (.Value <> "")
        Next
    ActiveSheet.Protect Password:="01234"
End With
End Sub
 
Upvote 0
Thank you for your advice and quick reply , and apologize as I'm new here

I'm getting this error could you please check

1597476319876.png
 
Upvote 0
Sorry, I missed an End If
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel As Range
With Target
    If .Column = 1 Or .Column = 2 Then
        If .Row > 1 Then
                ' time stamp corresponding to cell's last update
            Cells(.Row, 4).Value = Now
                ' Windows level UserName | Application level UserName
            Cells(.Row, 3).Value = Environ("username") & "|" & Application.UserName
            Range("C:D").EntireColumn.AutoFit
        End If
    End If
    ActiveSheet.Unprotect Password:="01234"
        For Each cel In .Cells
            .Locked = (.Value <> "")
        Next
    ActiveSheet.Protect Password:="01234"
End With
End Sub
 
Upvote 0
thanks you but I don't know why still i face some errors , if you still there to help please check the below

I'm still not getting the user name and getting this error instead

Note: Once I fill the request # i need to get the now date and username then locking the filled cells

1597484285048.png
 
Upvote 0
I don't see an error, at the moment the cell in column I is empty because you have stopped the code before the name has been entered.
 
Upvote 0
Thank you , it's working fine now , the cells were protected before entering the values,

thanks for your support.
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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