passing arguments and function results

almagg

Well-known Member
Joined
Aug 21, 2002
Messages
705
since i have this in several macros i wanted to create a function that i can call.
the status bar argument uses two variables: timeremaining and labels. the second is for when the time goes below 1 min. then the label is changed to sec. the 'timeremaining' calculation uses the two variables time1 and time2.

now since the default is 'by ref', could i pass three variables to a function, the third being timeremaining which will be updated by the function. then since the function can only return one variable all i need to pick up is 'label'.

finally how do i get the result back into the calling procedure?

sub
.
status time1, time2, timeremaining
.
endsub

function status(time1,time2,timeremaining)

(stuff happens)
timeremaining = (this will update the timeremaining variable in the procedure?)

if timeremaining<1 then
labels = "sec."
endif

then what to do for the above procedure to get 'labels'?

thanks
al
This message was edited by almagg on 2002-10-30 15:18
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
To return the results of a function I believe you just set the function equal to a variable, like:

variable = status(time1,time2)
 
Upvote 0
Here is how to get the result of a function

first you must declare the function to return a result like this

function functionName(arg1,arg2,arg3) as XXXX
where XXXX is the data type of the value you want to return.

Second the result that is returned is returned via a variable that has the same name as the function.
so if you want functionName to return the value 10 which is the value of a variable named fuu put the following as the last line of the function (before the End function line)
functionName = fuu

finally when you call the function you must set a variable equl to the function e.g.

bar = functionName(var1,var2,var3)

in this case bar would now equal 10.

Hope this helps
Gary

garyaebert@netscape.net
 
Upvote 0
thanks, that makes sense BUT
now i am getting this error message when i start the macro:
compiler error
"ambiguous name detected: status"

and the highlight in the editor is on the Function line itself, not in the procedure when it is called.

here's what i have:

Function status(time1,time2) as Var
.
timeremaining = status(time1,time2)
.


Function status(time1,time2)
.
status = timerem
.
End Function

al
This message was edited by almagg on 2002-10-30 17:39
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,478
Members
448,967
Latest member
visheshkotha

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