ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Help With A Variable And Functions (https://www.excelbanter.com/excel-programming/442600-help-variable-functions.html)

Andy

Help With A Variable And Functions
 
Hi Gang

I need some help with variables with functions. I've got a main
function that does some processing with a variable MyValue. I need to
create a sub function that changes the value of MyValue so that when
the sub function is done the main function knows what the new value of
MyValue is. How do I do this???

Please help,
Andy


Public Sub MyMainFunction()
MyValue = 10
Call MySubThatChangesMyValue

...more processing that relies on the changed MyValue...
End Sub

------------------------------------------------

Sub MySubThatChangesMyValue()
....some code that changes MyValue....
End Sub

Don Guillett[_2_]

Help With A Variable And Functions
 
'=================
Public myvalue
Public Sub MyMainFunction()
MsgBox myvalue
Call MySubThatChangesMyValue
MsgBox myvalue
End Sub
Sub MySubThatChangesMyValue()
myvalue = 35
'...some code that changes MyValue....
End Sub
'=============
--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Andy" wrote in message
...
Hi Gang

I need some help with variables with functions. I've got a main
function that does some processing with a variable MyValue. I need to
create a sub function that changes the value of MyValue so that when
the sub function is done the main function knows what the new value of
MyValue is. How do I do this???

Please help,
Andy


Public Sub MyMainFunction()
MyValue = 10
Call MySubThatChangesMyValue

...more processing that relies on the changed MyValue...
End Sub

------------------------------------------------

Sub MySubThatChangesMyValue()
...some code that changes MyValue....
End Sub



Rick Rothstein

Help With A Variable And Functions
 
I would add an argument to and pass MyValue to the MySubThatChangesMyValue
subroutine ByRef so that changes it makes are reflected back to the calling
program.

Public Sub MyMainFunction()
MyValue = 10
Call MySubThatChangesMyValue(MyValue)
......more processing that relies on the changed MyValue...
End Sub

Sub MySubThatChangesMyValue(ByRef VariableIn As Double)
....some code that changes MyValue....
End Sub

I made a guess at the data type for MyValue as Double, but you can change it
to match your declaration back in MyMainFunction (you do declare your
variable as to data type, right?).

--
Rick (MVP - Excel)



"Andy" wrote in message
...
Hi Gang

I need some help with variables with functions. I've got a main
function that does some processing with a variable MyValue. I need to
create a sub function that changes the value of MyValue so that when
the sub function is done the main function knows what the new value of
MyValue is. How do I do this???

Please help,
Andy


Public Sub MyMainFunction()
MyValue = 10
Call MySubThatChangesMyValue

...more processing that relies on the changed MyValue...
End Sub

------------------------------------------------

Sub MySubThatChangesMyValue()
...some code that changes MyValue....
End Sub



Helmut Meukel

Help With A Variable And Functions
 
As Don already suggested, you can declare MyValue as Public,
or you use MyValue as parameter in your call to the sub.
Call MySubThatChangesAValue(MyValue)

Sub MySubThatChangesAValue(ByRef aValue)
...some code that changes MyValue....
End Sub

Note that MyValue is passed by reference (the default for most
data types), not by value. Thus changes made to aValue in
the sub are there in MyValue when returning from the sub.
Two advantages compared with the "Public"approach: MyValue
is not visible/accessible for other subs/functions and you can call
the sub from another function to perform the same changes to
another variable e.g.
Call MySubThatChangesAValue(MySecondValue)
Even in such a scenario a public variable might work, but then
you have to assign the different values to MyValue and keep track
of what MyValue holds. Error prone.

Helmut.


"Andy" schrieb im Newsbeitrag
...
Hi Gang

I need some help with variables with functions. I've got a main
function that does some processing with a variable MyValue. I need to
create a sub function that changes the value of MyValue so that when
the sub function is done the main function knows what the new value of
MyValue is. How do I do this???

Please help,
Andy


Public Sub MyMainFunction()
MyValue = 10
Call MySubThatChangesMyValue

...more processing that relies on the changed MyValue...
End Sub

------------------------------------------------

Sub MySubThatChangesMyValue()
...some code that changes MyValue....
End Sub





All times are GMT +1. The time now is 06:49 AM.

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