Using the same variable in multiple subs

Yamezz

Active Member
Joined
Nov 22, 2006
Messages
343
Office Version
  1. 2019
When calling subs from a main sub, how do we handle variables? For example, I would like to declare and set a filename variable just once (selected by the user on the fly) in the main sub and re-use that variable for the subsequent subs that perform different operations on the input file. However, I take it each time Excel gets to an End Sub command it promptly forgets the variable and I will have to have the user select the input file over and over in each subsequent Sub.
Ideally, I'd like to do the following:
VBA Code:
Sub MonthlyImport()
    Dim MonthlyDataFile As Variant
    MonthlyDataFile = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls*), *.xls*")

    Call Subroutine1
    Call Subroutine2
    Call Subroutine3

End Sub

Sub Subroutine1()
    'Perform operations on MonthlyDataFile
End Sub

Sub Subroutine2()
    'Perform more operations on MonthlyDataFile
End Sub

Sub Subroutine3()
    'Perform yet more operations on MonthlyDataFile
End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
You can set at top of the module (after "Option Explicit" and before any sub or function):
VBA Code:
Private MonthlyDataFile As Variant
And remove from sub: Dim MonthlyDataFile As Variant.
Note that the private variable once value is set will retain the value until it's set again.
 
Last edited:
Upvote 0
Or pass it as an argument:

VBA Code:
Sub MonthlyImport()
    Dim MonthlyDataFile As Variant
    MonthlyDataFile = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls*), *.xls*")

    Call Subroutine1(MonthlyDataFile )
    Call Subroutine2(MonthlyDataFile )
    Call Subroutine3(MonthlyDataFile )

End Sub

Sub Subroutine1(MonthlyDataFile as string)
    'Perform operations on MonthlyDataFile
End Sub

Sub Subroutine2(MonthlyDataFile as string)
    'Perform more operations on MonthlyDataFile
End Sub

Sub Subroutine3(MonthlyDataFile as string)
    'Perform yet more operations on MonthlyDataFile
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,656
Messages
6,126,055
Members
449,284
Latest member
fULMIEX

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top