Passing Variables between Macros

christian2016

Board Regular
Joined
Oct 6, 2016
Messages
123
Hi Guys,

Unsure if you are able to do this.

Example
1. Macro - Sub Test
2. Macro - Sub Test2

Within macro "Test " I'm calling macro "Test2" and passing a variable. The called macro "Test2" does what it needs to do and once completed I need to pass variables back to the main macro "Test" which will use these variables to complete the rest of the code in "Test".

Any help is greatly appreciated.

Thanks
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
You could do something like this

VBA Code:
Sub Macro1()
    Dim aVar as Variant
    Dim bVar as Variant

    aVar = 12

    Call Macro2(aVar, bVar)

    MsgBox bVar
End Sub

Sub Macro2(inputValue as Variant, byRef outputValue as Variant)
    outputValue = inputValue + 5
End Sub

or you could cast Macro2 as a function rather than a sub.
 
Upvote 0
Solution
You could do something like this

VBA Code:
Sub Macro1()
    Dim aVar as Variant
    Dim bVar as Variant

    aVar = 12

    Call Macro2(aVar, bVar)

    MsgBox bVar
End Sub

Sub Macro2(inputValue as Variant, byRef outputValue as Variant)
    outputValue = inputValue + 5
End Sub

or you could cast Macro2 as a function rather than a sub.
Thank you. This seems to work well
 
Upvote 0

Forum statistics

Threads
1,215,692
Messages
6,126,228
Members
449,303
Latest member
grantrob

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