VBA Set value accessible in other subs

jmpmolegraaf

New Member
Joined
Jul 10, 2013
Messages
35
Hello everybody,

So far you've all been of great help and now I want to figure out something else.

I have a lot of cases were I set a value/range in one sub but actually need it in another sub again. See example below. How can make set values/ranges/etc available in multiple subs?

Thanks!


Sub Example ()

set r = range ("A1")

End Sub


Sub Example2 ()

' i want to use r here

End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Code:
[COLOR=#ff0000]Dim r[/COLOR] ' before Sub
Sub Example ()
set r = range ("A1")
End Sub

Sub Example2 ()
' i want to use r here
End Sub
 
Upvote 0
it's not the Dim part that's a problem.. i always define them before the subs start.

but the value/range of "r" seems to be reset when leaving sub 1 while needing it in sub 2.
 
Upvote 0
Hello Patel, example below:

Option Explicit


Dim wsNLD As Worksheet
Dim wsGP As Worksheet

Sub NonListedPTDiscounts()


Set wsNLD = ThisWorkbook.Sheets("Non-Listed Discounts")
Set wsGP = ThisWorkbook.Sheets("GP Matrix")

etc etc

End Sub

----------

Sub NonListedPDiscounts()


Set wsNLD = ThisWorkbook.Sheets("Non-Listed Discounts")
Set wsGP = ThisWorkbook.Sheets("GP Matrix")



etc etc

End Sub

-------


It seems that I have to set these worksheets again in the next sub. Both subs are in the same module and their dimensions are determined "before" the first sub.
 
Upvote 0
Hi, jmpmolegraaf,

maybe try it like this and initialize the variables in their own routine:
Code:
Dim wsNLD As Worksheet
Dim wsGP As Worksheet

Sub MainInitialize()
Set wsNLD = ThisWorkbook.Sheets("Non-Listed Discounts")
Set wsGP = ThisWorkbook.Sheets("GP Matrix")
End Sub

Private Sub NonListedPTDiscounts()
If wsNLD Is Nothing Then MainInitialize
Debug.Print wsNLD.Name
MainExit
NonListedPDiscounts
End Sub

Private Sub NonListedPDiscounts()
If wsGP Is Nothing Then MainInitialize
Debug.Print wsGP.Name
MainExit
End Sub

Sub MainExit()
Set wsNLD = Nothing
Set wsGP = Nothing
End Sub
The variables have been set as local variables for a module, if the code is in different modules you would need to declare them Public in a standard module.

@patel45:
2 answers with identical test - I´m impressed on your will to post ...I mean on the will to help (your posting account to get up).

Ciao,
Holger
 
Upvote 0
Thanks a lot Holger.. that looks promising.

To keep things clean and easy to understand, I want to create one MasterSub in which I "set" the main things and then I call all the various subs for each tasks that have to be done.
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

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