Using Sub-procedures w/in a function

L

LizUrish

Is it possible to use a subprocedure within a function?
How would you refer to the subprocedure?

I'm trying to create a variable in a subprocedure that is
updated w/ a function, so that the vairable holds the
value and can be used the next time the function is called
upon.

Any help would be greatly appreciated!
 
K

Kevin Beckham

Either define the variable at a global level, e.g.
Option Explicit
Dim myVar1 as Integer 'can be used by any Sub or
Function on this sheet, retains its value between uses
Public myVar2 as Integer 'can be used by any Sub or
Function in this project, retains its value between uses

:
:

else define the varibale as static within the function,
e.g.

Function TestIt() As Integer
Static myVar3 As Integer
myVar3 = InputBox("Enter a number", "Test it", myVar3)
TestIt = myVar3
End Function

then type
?Testit
in the Immediate pane a couple of times (Ctrl-G if it's
not open)

Kevin Beckham
 
H

Harald Staff

Another approach to remember values is to declare a Static value in a sub:

Sub test()
Static L As Long
If L = 0 Then L = 1
L = Doubled(L)
MsgBox "L is now " & L
End Sub

Function Doubled(X As Long) As Long
Doubled = X + X
End Function

I'm not too sure what your question really is. If you want to pass
variables, it's done like above. If you want to run a Sub from a function in
a cell, no that is not possible.
 

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

Top