ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Using values from one macro in another! (https://www.excelbanter.com/excel-programming/294523-using-values-one-macro-another.html)

aiyer[_11_]

Using values from one macro in another!
 
Hi all!

I've assigned a value to a variable (X) declared in a certain macro.
was wondering if there would be a way for me to use the VERY SAM
variable X (which has the stored value in it) in another macro?

I would appreciate any help.

Thanks,

Arun.....
VTec corp

--
Message posted from http://www.ExcelForum.com


Bob Phillips[_6_]

Using values from one macro in another!
 
Declare it as a Public variable in a standard module, outside of any macro,
at the top of the code.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"aiyer " wrote in message
...
Hi all!

I've assigned a value to a variable (X) declared in a certain macro. I
was wondering if there would be a way for me to use the VERY SAME
variable X (which has the stored value in it) in another macro?

I would appreciate any help.

Thanks,

Arun.....
VTec corp.


---
Message posted from http://www.ExcelForum.com/




Rob van Gelder[_4_]

Using values from one macro in another!
 
You can turn the variable into a global variable so that both procedures can
access it:
eg.

Dim myvar As Long

Sub test1()
myvar = 123
End Sub

Sub test2()
MsgBox myvar
End Sub


--
Rob van Gelder - http://www.vangelder.co.nz/excel


"aiyer " wrote in message
...
Hi all!

I've assigned a value to a variable (X) declared in a certain macro. I
was wondering if there would be a way for me to use the VERY SAME
variable X (which has the stored value in it) in another macro?

I would appreciate any help.

Thanks,

Arun.....
VTec corp.


---
Message posted from http://www.ExcelForum.com/




onedaywhen

Using values from one macro in another!
 
Pass it to the second macro as an argument. If the second variable
need to use the variable itself (i.e. potentially change its value)
pass it by reference, otherwise pass it by value:

Sub Macro1()
Dim X As Long
X = 2
Macro2 X
Macro3 X
End Sub

Sub Macro2(ByVal Value As Long)
MsgBox CStr(Value)
End Sub

Sub Macro3(ByRef arg As Long)
arg = arg + 10
MsgBox CStr(arg)
End Sub

--

aiyer wrote in message ...
Hi all!

I've assigned a value to a variable (X) declared in a certain macro. I
was wondering if there would be a way for me to use the VERY SAME
variable X (which has the stored value in it) in another macro?

I would appreciate any help.

Thanks,

Arun.....
VTec corp.


---
Message posted from http://www.ExcelForum.com/



All times are GMT +1. The time now is 02:01 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com