• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Using VBA to Auto Populate User Name

Diane

New Member
I want to have the user name from Microsoft Office populate in cell F14. I have never written VBA code before.

I put this code into visual basic module:


Function GetUserName()

GetUserName = Application.UserName

End Function


Then in F14 I put

=GetUserName()


What am I missing??
 
Everything looks right. Did you put the function in a regular VBA module, not a sheet module?


What is the result of your formula? (error message?)
 
#NAME is the error message. Shoulda stated that, sorry. Hmmm...I went in and checked and it says Sheet12. When I created it I went to my spreadsheet and double clicked the "Visual Basic" icon. Then I pasted the code in.
 
I think your function is in the wrong place. In the VB editor, go to Insert - Module, and put your function there. Refresh the formula in the workbook, and you should have success.
 
OK Luke, I did that. I'm getting the same error. I don't know diddly about writing VBA code this is my first attempt. Do I have to save the code first? If so, how do I do that?
 
Hmm. No, you don't need to really "save" the code, it's a part of the XL workbook. I.e., if you want to "save" it, just save the XL workbook.


The #NAME! error is indicating that the workbook is finding the function in the correct spot. Running through from the top:

1. Start in the XL workbook

2. Right-click on the worksheet tab, select "view code"

3. You should now be in the worksheet module, in the VBE. Goto Insert - Module.

4. A new module should be created for your workbook. Paste in:

Code:
Function GetUserName()

GetUserName = Application.UserName

End Function

5. Close the VBE, and in the workbook try the formula again:

=GetUserName()
 
Um....just went to save it and now the #NAME? error is back in F14 again. I didn't rename it, just saved it as same file (.xlsm)
 
Are there any other workbooks open or something that might be interfering? Everything you've done so far should be working...

After you save the file, the code is still there in the VBE, correct?

(Side note, hope this conversation doesn't sound "help desk condescending". I'm truly just befuddled as to what's wrong)

=)
 
Yeah, the code is still "=GetUserName()" after I save it. One thing tho...I notice that the type ahead feature brings up lots of suggestions for putting my code in F14 like "getpivotdata" but "GetUserName" does not pop up in the type ahead suggestions.


Oh..I don't think you are condescending. :)
 
Bah...This shouldn't actually make a different, but what if you change the function name to something simple, like "test" and then change the formula in your workbook?


Also, if it's possible to bring up the "formula wizard" (in 2003, I can click on the Fx button to left of formula bar), you're function should be in the User Defined category.
 
Here is what I just tried:


Function test()

test = Application.UserName

End Function


Same result. I did find 3 functions in the User Defined category. They are all the same cuz I've been working to get it right. Should I delete 2 of them? If so how do I delete them? :)
 
Yea, you should delete the others so that's there's only 1 function with the unique name. Simply select the text/code in the VBE and hit the delete key.
 
Back
Top