How can I pass variables betweet two Subs in Excel VBA?

CorinneGermany

I know how to pass the variables between two Subs when calling a sub running the action from within the Sub containing the variables set up, but I want to call the variable setup Sub from within the routine macro but can not get the correct place to declare the variables to get it to work. I want to have the Variable set up separate to be called by other Subs. The code below does not work as the two subs

Sub X_Variables(s1, s2, s3, rng1, rng2, rng3)
'

' x - Defined Cell Names  - ANFlightText , AUFlightText , ABFlightText

Dim s1   As Worksheet
Dim s2   As Worksheet
Dim s3   As Worksheet
Dim rng1  As Range
Dim rng2  As Range
Dim rng3  As Range

Set s1 = Sheets("5_Angebot")        ' WorkSheet
Set s2 = Sheets("5_Auftragsb")      ' WorkSheet
Set s3 = Sheets("5_Abschluss")      ' WorkSheet
Set rng1 = s1.Range("B15")          ' End on cell
Set rng2 = s2.Range("B15")          ' End on cell
Set rng3 = s3.Range("B15")          ' End on cell

End Sub

Called by

Sub X_Offer_Hide_Flight_Text()
'
' ***** Hide Flight Text  *****

' x - Defined Cell Names  - ANFlightText , AUFlightText , ABFlightText
' x (s1, s2, s3, rng1, rng2, rng3)


'  ***** Set up veriables *****

Call X_Variables

' *****

' Hide rows
Application.ScreenUpdating = False  ' do not see screen updating

s1.Select
Range("ANFlightText").Select        ' x
Selection.EntireRow.Hidden = True
rng1.Select

s2.Select
Range("AUFlightText").Select        ' x
Selection.EntireRow.Hidden = True
rng2.Select

s3.Select
Range("ABFlightText").Select        ' x
Selection.EntireRow.Hidden = True
rng3.Select

s1.Select

Application.ScreenUpdating = True  ' see screen updating

End Sub
Christoph Corleis

You have to declare the variables as global, outside of the sub.

public s1   As Worksheet
public s2   As Worksheet
public s3   As Worksheet
public rng1  As Range
public rng2  As Range
public rng3  As Range

Sub X_Variables()
'

' x - Defined Cell Names  - ANFlightText , AUFlightText , ABFlightText

Set s1 = Sheets("5_Angebot")        ' WorkSheet
Set s2 = Sheets("5_Auftragsb")      ' WorkSheet
Set s3 = Sheets("5_Abschluss")      ' WorkSheet
Set rng1 = s1.Range("B15")          ' End on cell
Set rng2 = s2.Range("B15")          ' End on cell
Set rng3 = s3.Range("B15")          ' End on cell

End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Excel VBA combine two SUBs

How can I pass two variables to a view?

MS Excel VBA: Is it faster to use Public Variables or Pass Variables between Subs?

How Can I pass multiple VBA variables into a called Powershell script?

How can i use enum to pass variables between two scripts?

How can I get the length of a two dimensional array in VBA (excel)?

How can I create a two column combobox on a userform in Excel VBA using the first two columns of a table?

How can i pass variables in Zend?

Using ajax how can I pass variables to two different php scripts

How can I pass variables back and forth between two classes in PyQt5?

How can I use Excel VBA to select certain ranges depending on input of variables

How can I pass parameter in the laravel excel?

How can I couple the lifetimes of two variables?

How can I subtract two NSUInteger variables?

How can I reference two different sheets in an excel file from word using vba?

Excel VBA how can I find where two blank rows appear and delete one of those rows?

How can I loop through a list in Excel VBA and concatenate unique and duplicate IDs from two columns?

How can I loop through a list in Excel VBA and concatenate unique and duplicate IDs & Qtys from two columns?

VBA - Excel: How can I optimize this code?

How can I optimize this code in Excel VBA?

How can I use coderefs instead literal subs when there is a `&` prototype?

How can I mix two formula in excel?

How can I automatically pass variables out of the function? How to pass variables from inner to the outer scope?

How to pass two variables with selectors to $()

Simultaneously updating two variables in an Excel VBA for loop

How can I pass variables into a Powershell ObjectEvent action

How can I pass around variables between handlers

How can I pass Express.js variables to MongoDB functions?

AngularJS: How can I pass variables between controllers?