Macro assistance - inserting user name but cell not updating when workbook opened by another staff member

Aussie5380

New Member
Joined
Sep 30, 2022
Messages
32
Office Version
  1. 2019
Platform
  1. Windows
Hi There,

I have been provided with the below macro, but im having a little trouble with the user name not updating when the workbook is opened by another staff member.

The code i have is below:

Public Function UserName()
Application.Volatile
UserName = Environ$("UserName")
End Function

I have tried to add the "Application.Volatile" bit, but it doesnt update the range of cells i need.

The cells where the user name is to generate is:

D11:F11 (merged cells)

The code in the cells is:

=VBAProject.WELCOME.UserName()

When someone else opens it, it has my name listed, where it should have the current users name (as its linked to checklist generators and saves the staff having to write their name)

Is anyone able to assist, im just not sure what else i can try?

Many thanks,

Shaun
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Step through the code and check your results. Or add Debug.Print UserName before End Function. If you see nothing in the immediate window, that's your issue - the value isn't being over-written. Unfortunately I don't know how to code to write a value to a group of merged cells so maybe that's something to look at if you get a result in the immediate window. FWIW I would never name a procedure (or any object for that matter) using another function name (Environ) or any of its named variables (e.g. UserName).
 
Upvote 0
Instead of using a UDF, why not use the workbook open event to write the username to the cell you want?

Example (not tested)
VBA Code:
Private Sub Workbook_Open()
    Me.Worksheets("Sheet1").Range("D11").Value = Environ$("UserName")
End Sub


(Tip: For future posts , you should try to use code tags like I did above when posting your code. It makes it easier to read.)

 
Upvote 0
Solution
Instead of using a UDF, why not use the workbook open event to write the username to the cell you want?

Example (not tested)
VBA Code:
Private Sub Workbook_Open()
    Me.Worksheets("Sheet1").Range("D11").Value = Environ$("UserName")
End Sub


(Tip: For future posts , you should try to use code tags like I did above when posting your code. It makes it easier to read.)

Thank you, and apologies, i will use the code tag next time :) i have seen this code before, im just unsure where i pop this? Do i just add it to a new normal module?

One last question - the Sheet name, is that what i have actually named the sheet? (FRONT COVER) or is it the sheet number listed in the VBA writer (Sheet4)

once again, thank you for your assistance
 
Upvote 0
Step through the code and check your results. Or add Debug.Print UserName before End Function. If you see nothing in the immediate window, that's your issue - the value isn't being over-written. Unfortunately I don't know how to code to write a value to a group of merged cells so maybe that's something to look at if you get a result in the immediate window. FWIW I would never name a procedure (or any object for that matter) using another function name (Environ) or any of its named variables (e.g. UserName).
Thank you! adding the "Debug.Print UserName " has fixed it! You sir are a scholar and a gentleman!

Thanks everyone for your assistance. Its greatly appreciated!
 
Upvote 0
The debug didn't really fix it, it could only reveal the cause? What was the cause - no value in variable UserName (and what did you do to fix it if that was the problem)?
 
Upvote 0
im just unsure where i pop this? Do i just add it to a new normal module?
It goes in the workbook code module
1669780202255.png



One last question - the Sheet name, is that what i have actually named the sheet? (FRONT COVER) or is it the sheet number listed in the VBA writer (Sheet4)
It's what you actually named the sheet (i.e "Sheet1","Main", "Data", etc.)
 
Upvote 0
The debug didn't really fix it, it could only reveal the cause? What was the cause - no value in variable UserName (and what did you do to fix it if that was the problem)?
Im not sure what has fixed it then. The code looks like this:

VBA Code:
Public Function UserName()
Application.Volatile
UserName = Environ$("UserName")
Debug.Print UserName
End Function

(apologies, im not sure how to use the code tag?)

the cell calls this macro via this code:

=VBAproject.ADDNAME.UserName()

When another staff member opens it, it now shows their name! The only thing i can think of is there must have been something wrong with the module naming as thats the only other thing i have changed
 
Upvote 0
You could rem out (comment out) the debug line as it's only for troubleshooting. It did nothing to fix your problem. It had to be something else.
BTW, I agree with setting the cell value in the open event as was suggested. That's probably where the solution credit should go.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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