Link to home
Start Free TrialLog in
Avatar of ToddHawley4984
ToddHawley4984

asked on

Assigning a value to a variable in another procedure

I have been  using excel vba to develop some forms...I have been using text box values as values in my comutations...However since in some cases i need to format the text to a given decimal place i am loosing precision. So my solution is to assign the values to variables and format during the transision from variable to text box....for instance In the example below i am assigning a value to a text box in another form...along these lines how would i assign a value to a variable in a private sub linked to a different form..


'rad is a double variable 
 
 
Time_of_Concentration_Form.tb_hydro_rad_CHF5 = rad
'this works fine
 
 
Time_of_concentration_form.private sub main (local Variable) = rad
'this i can't do

Open in new window

Avatar of PaulHews
PaulHews
Flag of Canada image

Your local variable has local scope and is inaccessible from outside the form.  The preferred method to keep state in your userform is to use a module level variable and access it with a public property

In your user form:

Private mRad As Double

Private Sub Main()
    mRad = 5  'Set however you set this variable.
End Sub

Public Property Get Rad() As Double
    Rad = mRad
End Property

Public Property Let Rad(Value As Double)
    mRad = Value
End Property


Now you can assign the value using the public property from other code:

Time_of_Concentration_Form.Rad = rad




Avatar of ToddHawley4984
ToddHawley4984

ASKER

I've only been programming in VBA for about a week...I understand that its a module level variable but I don't quite understand how to manipulate it

If i'm gonna use a given value in different form should i use a public variable delared in my module?  Is there any real reason not to use public variables other than memory consumption?  I realize if i have a public variable i have to be careful about changing in from different procedures
ASKER CERTIFIED SOLUTION
Avatar of PaulHews
PaulHews
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial