Home > database >  Pass Input Box variable from one sub to another
Pass Input Box variable from one sub to another

Time:09-22

I was reading a post by another where the need for a Global declaration wasn't needed.

I have a Sub asking for the user to enter a value, in another Sub I use the entered value to Call other Subs based on their input.

Am I close in my example? Thank you

Option Explicit

Sub MonthTest()
Dim strMonth As String

strMonth = InputBox("enter Area here")

Call Test2

End Sub

Sub Test2(strMonth As String)

Select Case strMonth
    Case Is = "82"
        Call Area82
    Case Is = "80"
        Call Area80
    
End Select
End Sub

CodePudding user response:

you must feed in the variable to the calling function, else it does not know where to look for it.

change: Call Test2 to: Call Test2 strMonth

CodePudding user response:

Replace 'Call Test2withTest2 strMonth, there's no need to use Call`

Variable declared in a sub/function stays in the sub/function itself only, since you have declare a parameter in Test2, you will need to provide the parameter everytime you call Test2 (unless its declare as Optional).

CodePudding user response:

x1up should be xlup (lower case "L", not the #1)

it is a constant defined in the EXCEL COM library. Hit F2 and look under the EXCEL library (which you should have in your references in VBA). You should see the xlup constant defined in the (general) section of the library, or under a particular ENum name.

  • Related