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

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
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,831
Messages
6,127,145
Members
449,363
Latest member
Yap999

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