1

I have not used VBA at all and was wondering if anyone could help me with automatically running a macro when opening a workbook? I get an error when opening the workbook stating:

"Run-time error '424': Object required"

Here is the code I tried:

Private Sub Workbook_Open()
    GetUserName = Application.username
End Sub
Function GetUserName()
    GetUserName = Application.username
End Function

2 Answers 2

7

Not sure what you are wanting to achieve, but this perhaps?

Private Sub Workbook_Open()
    Sheet1.Range("A1").value=GetUserName
End Sub
Function GetUserName()
    GetUserName = Application.username
End Function
Sign up to request clarification or add additional context in comments.

14 Comments

I want the username to appear in a cell within the workbook, not as a msgbox. I appreciate your help.
@asnf21395 - you didn't say that, I've amended the code.
I just tried the new code but the cell isn't changing. Is there anything else I can try?
Works for me. Have you definitely put the Open code in the ThisWorkbook module?
Yeah, it is pulling the user name but it isn't automatically updating when I reopen the workbook.
|
1

To the solution above - the code can be shorten to:

Private Sub Workbook_Open()
    Sheet1.Range("A1").Value = Application.UserName
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.