Share via

Calling a public function from another Macro

Anonymous
2019-04-11T10:09:33+00:00

Hi There

I am currently attempting to use this VBA on the 'ThisWorkbook' project page:

Private Sub Workbook_Open()

Worksheets("Overview").Activate

Call UserName

End Sub

To call this function:

Public Function UserName()

    UserName = Environ$("UserName")

End Function

to display the current user name in cell A15 using this formula:

=UserName()

But it doesn't seem to be working.

Is that something that should work?

Microsoft 365 and Office | Excel | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2019-04-11T13:48:59+00:00

Luke,

Make sure that "Option Private Module" is not used in Module1.

You don't need to call the function in the workbook_open sub.

Entering the formula in any cell should return the user name.

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2019-04-11T12:55:03+00:00

    Hi there

    The Public Function is in Module1, The 'Call UserName' code is on the 'ThisWorkbook' Project.

    Many thanks

    Luke

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2019-04-11T11:53:03+00:00

    LudeJS,

    RE:  public functions

    Public functions should be in a standard module as worksheet and workbook modules are private.

    '---

    Custom_Functions add-in (19 of them)

    Download from MediaFire...

    http://www.mediafire.com/folder/lto3hbhyq0hcf/Documents

    Was this answer helpful?

    0 comments No comments