How To Use Variables Across Multiple Sub Routines (VBA)

JoshuaKC29

New Member
Joined
Apr 20, 2019
Messages
10
I was wondering if there was anyway to carry variables across multiple sub routines. What I want to do is set a macro to a button, and I want to use an integer to make small changes to the main sub routine.

I know I can accomplish this by making the button change a cell in excel, then making reference to that cell and setting it to 0 when completing the sub routine. But I'd prefer to do this all within VBA is possible.

An example of what I'd like to accomplish is declaring BtnValue as an Integer. When I press a button, it sets the BtnValue to X then calls the main subroutine which produces a message.

If X = 1 then message 1 is sent.
If X = 2 then message 2 is sent.

Just an idea I had in a my sleep and I want to know if its possible.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Yes, you use parameters like this:

Rich (BB code):
Public Sub MyMacro()
    MacroClick 2
End Sub

Public Sub MacroClick(ByVal inValue as Long) 
    Msgbox inValue
End Sub
 
Last edited:
Upvote 0
Two ways:
1. Declare them outside any sub.
Code:
Option Explicit
Dim ANum as Long, BTxt as String

Sub Main()
ANum = 1
BTxt = "A"
Call Slave
End Sub

Sub Slave()
Msgbox ANum & vbCr &  BTxt
End Sub
2. Pass them from one sub to the next
Code:
Sub Main()
Dim ANum as Long, BTxt as String
ANum = 1
BTxt = "A"
Call Slave(ANum, BTxt)
End Sub

Sub Slave(XNum as Long, YTxt as String)
Msgbox XNum & vbCr & YTxt
End Sub
 
Upvote 0
Much thanks to all,

I thought I was using the methods posted in this forum but I wasn’t getting the desired result. That being said, when I got to work those “same methods” worked. I must have been half asleep given it was about 5am when I was looking into this.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,169
Members
453,021
Latest member
Justyna P

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