Solved

Assigning  a value to a variable in another procedure

Posted on 2007-12-06
4
1,350 Views
Last Modified: 2012-06-22
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

0
Comment
Question by:ToddHawley4984
  • 2
  • 2
4 Comments
 
LVL 38

Expert Comment

by:PaulHews
ID: 20423552
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




0
 

Author Comment

by:ToddHawley4984
ID: 20423684
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

0
 

Author Comment

by:ToddHawley4984
ID: 20423696
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
0
 
LVL 38

Accepted Solution

by:
PaulHews earned 50 total points
ID: 20424490
Using a public variable is a possibility.  The private with property method I showed above would work the same as if you declared a public variable:

Public Rad As Double

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

Memory consumption isn't much of an issue when we're talking about one double value.  It's only 8 bytes and you would still use that with a private module level variable.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now