Function variables

T

Todd Huttenstine

hey guys I have a function and need to preserve its value
once the function ends. I need to store this value as a
variable in another module. Is this possible?


Todd Huttenstine
 
F

Frank Kabel

Hi
just call your function from the other fucntion/procedure and store the
result in a variable. e.g.

Sub foo()
dim ret_value
ret_value = do_calculate(5)
msgbox ret_value
end sub

function do_calculate(i)
do_calculate = i*2
end function
 
B

Bob Phillips

Can't you just run the function again?

If not, save the result as a Module scope variable.

--

HTH

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

Harald Staff

Hi Todd

Like this ?

Function MyL(FinalDigit As Long) As Long
MyL = Second(Now) * 1000 + FinalDigit
End Function

Sub test()
Dim L As Long
L = MyL(3)
MsgBox L
MsgBox "Other things inbetween"
MsgBox L, , "Still here :)"
End Sub

HTH. Best wishes Harald
 
T

Todd Huttenstine

I cant get this to work. heres what I did...

Declare Function GetUserName Lib "advapi32.dll" _
Alias "GetUserNameA" ( _
ByVal lpBuffer As String, nSize As Long) As Long

Public Sub WindowsUserName()
Dim UName As String * 255
Dim nn
Dim L As Long: L = 255
Dim Res As Long
Res = GetUserName(UName, L)
UName = Left$(UName, L - 1)
nn = Trim(UName)
End Sub
 
F

Frank Kabel

Hi
and what's your problem. 'nn' contains the Windows user name which you
could check with an added statement like
msgbox nn
 
T

Todd Htutenstine

Because the username function is in another module and I
need to reference variable nn.
 
F

Frank Kabel

Hi Todd
why not create an additional function like

Public Function Get_Str_UserName()
Dim UName As String * 255
Dim nn
Dim L As Long: L = 255
Dim Res As Long
Res = GetUserName(UName, L)
UName = Left$(UName, L - 1)
nn = Trim(UName)
Get_Str_UserName = nn
End Function

Now call this function from your other module.
Another option would be to create a public variable and initialize this
variable in the workbook_open event. But I'd prefer using a function
like shjown above for this
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Static Variable 7
LastName, FirstName format 2
Global/Public/Universal variables 8
Trim "\" Value 7
VBA Count Function 2
Collection Object Keys 10
DIm Variables 4
All open projects 4

Top