View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
JLGWhiz JLGWhiz is offline
external usenet poster
 
Posts: 3,986
Default Automatically resest variable values = 0 after runtime

Once the procedure executes End Sub all variables are emptied anyhow. They
do not retain their values for a second run. Even if the variables are
public and your procedure calculates their value at each run, they would not
have to be reset to zero, unless as Charlie pointed out, you are adding the
existing value to something. In that case you could use an If statement to
say if it is greater than zero then make it zero.

"RyanH" wrote:

I have about 40 variables at the top of a Userforms Declaration section. I
do this because I need the variables calculated in one procedure and made
available to another procedure when they are all done being calulated.

The problem is I currently have to write code to reset all the variables
back to 0 before they are recalculated. If there a way to declare the
variable (DataType Single) = 0 at the beginning of runtime. For example:

Option Explicit

Dim var1 As Single = 0
Dim var2 As Single = 0

Private Sub CalculateVariables()

' code to calculate var1 & var2

End Sub

Private Sub UseVariables

' code to use var1 & var2

End Sub

After runtime var1 & var2 are reset to 0
--
Cheers,
Ryan