0

I have a macro I am constructing and I would like to call out different procedures present in the same macro sheet. Please try to solve it providing me the code, the order and the specific definition of the macro. Example:

Sub MyUserName()
dim UserName as String

UserName = "Alessio_110"
End Sub

Sub msgbox_1()
msgbox UserName
End Sub

In this example I would like to have a message box telling me the User Name I have set in a precedent procedure. How could I embed the two codes?

SJR
  • 22,986
  • 6
  • 18
  • 26
Alessio_110
  • 143
  • 10
  • 1
    Read this http://www.cpearson.com/excel/Scope.aspx Move your declaration of `UserName` outside the sub. – SJR Apr 05 '19 at 15:30
  • "Please try to solve it providing me the code, the order and the specific definition of the macro." - **GImmee the codez**? – AJD Apr 05 '19 at 19:25
  • se also https://stackoverflow.com/questions/11367662/how-can-i-keep-the-value-of-this-variable-for-my-next-subroutine/11367962#11367962 (the first example I could find) – AJD Apr 05 '19 at 19:33
  • see also https://stackoverflow.com/questions/16374253/how-to-make-excel-vba-variables-available-to-multiple-macros/16374516#16374516 – AJD Apr 05 '19 at 19:34
  • Possible duplicate of [How to make Excel VBA variables available to multiple macros?](https://stackoverflow.com/questions/16374253/how-to-make-excel-vba-variables-available-to-multiple-macros) – AJD Apr 05 '19 at 19:34

1 Answers1

0

To call a procedure you can just use its name. You pass the data as an argument to the called procedure:

So

    Sub MyUserName()
    dim UserName as String

    UserName = "Alessio_110"
    MyCalledProc UserName  'calles MyCalledProc and passes UserName as a reference
    End Sub

    Sub MyCalledProc(UserName as String)
        MsgBox UserName
    End Sub
Damian
  • 5,152
  • 1
  • 10
  • 21
Harassed Dad
  • 4,669
  • 1
  • 10
  • 12