Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a variable in one sub and use in another [duplicate]

Tags:

excel

vba

I am new to VBA programming and had a doubt which may be quite simple for you.

How do we set a variable in one sub which can be used in another?

I tried using global variable but it didnt work for me. Thank you

like image 616
VikkyB Avatar asked Jan 12 '23 18:01

VikkyB


1 Answers

Here is an example of how I have created a variable in one sub and used it in another:

    Private Sub txtLastName_LostFocus()
    FirstName = Me.txtFirstName.Value
    LastName = Me.txtLastName.Value
    FullName = FirstName & " " & LastName
    sayHelloToTheUser (FullName)
    End Sub

    Private Sub sayHelloToTheUser(name As String)
    MsgBox "Hello " & name
    End Sub

Essentially, you must pass it through using another sub and having it take the arguments that are necessary. This is the main way that I pass arguments through.

like image 104
Mardin Yadegar Avatar answered Jan 31 '23 10:01

Mardin Yadegar