asnf21395
asnf21395

Reputation: 19

Automatically run macro when opening workbook?

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

Upvotes: 1

Views: 1675

Answers (2)

PSotor
PSotor

Reputation: 346

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

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

Upvotes: 1

SJR
SJR

Reputation: 23081

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

Upvotes: 7

Related Questions