Problem with VBA code....

RandyD123

Active Member
Joined
Dec 4, 2013
Messages
289
Office Version
  1. 2016
Platform
  1. Windows
In the code below I am having an issue with some of the coding. I want the user name to show in column B and I want column B to clear if the user deletes or clears the info in column A. But at the same time I don't want the user to clear column B if there is an entry in column A.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)


On Error GoTo Catch

'// Prevent any changes firing this event again
Application.EnableEvents = False

'// Change the '1' to ignore any heading rows
1 If Target.Row > 1 Then

'// Which column was changed?
Select Case Target.Column
'// A
Case 1
Target.Offset(, 1).Value = "Changed By " & UserName & " on " & Date & " @ " & Time
Case 2
'// Prevent user changes to Col B
Application.Undo
Beep
End Select

End If

Catch:

'// Make sure event handling turned on again
Application.EnableEvents = True

End Sub

Any help would be much appreciated. Thank You
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi,
try updating this line

VBA Code:
Target.Offset(, 1).Value = "Changed By " & UserName & " on " & Date & " @ " & Time

to this

VBA Code:
Target.Offset(, 1).Value = IIf(Len(Target.Value), "Changed By " & Application.UserName & " on " & Date & " @ " & Time, "")

and see if does what you want

Dave
 
Upvote 0
Hi,
try updating this line

VBA Code:
Target.Offset(, 1).Value = "Changed By " & UserName & " on " & Date & " @ " & Time

to this

VBA Code:
Target.Offset(, 1).Value = IIf(Len(Target.Value), "Changed By " & Application.UserName & " on " & Date & " @ " & Time, "")

and see if does what you want

Dave


Nope, I gives me the name of my company, not the user who made the change.
 
Upvote 0
Nope, I gives me the name of my company, not the user who made the change.

Updated code is based on your post to clear Cell in Column B if Cell in Column A deleted which it should do.

Application.UserName is the user name which is set by the user when they run the program for the first time.
if you go to File > OPTIONS > GENERAL and set it under User Name. Clearly, it has been set with your company name

Change Application.UserName

to

Envirion("UserName")

which should show users network name

Dave
 
Upvote 0
Updated code is based on your post to clear Cell in Column B if Cell in Column A deleted which it should do.

Application.UserName is the user name which is set by the user when they run the program for the first time.
if you go to File > OPTIONS > GENERAL and set it under User Name. Clearly, it has been set with your company name

Change Application.UserName

to

Envirion("UserName")

which should show users network name

Dave
It errors out on Envirion (sub or function not defined)
 
Upvote 0
Updated code is based on your post to clear Cell in Column B if Cell in Column A deleted which it should do.

I don't see that in "updated" code?

And thanks for trying to help.....
 
Upvote 0

Forum statistics

Threads
1,215,002
Messages
6,122,652
Members
449,092
Latest member
peppernaut

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