| Home | Forums | Reviews | Articles | Register |
![]() |
| Thread Tools | Rate Thread |
|
|
|
| |
|
Charlie
Guest
Posts: n/a
|
Why exactly do you need to reset ALL of them? In a situation where a value
is used to recalculate itself, such as n = n + 1 then, yes, you need to reset. But for most calcs a = b + c you would not need to reset "a" first. And I presume "b" and "c" would be obtained from some external source, and THEY would not need to be reset first either. Post some code. "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 |
|
||
|
||||
|
JLGWhiz
Guest
Posts: n/a
|
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 |
|
||
|
||||
|
RyanH
Guest
Posts: n/a
|
I realize what you are saying Charlie and JLGWhiz, and I agree. I apologize
for not posting code. I basically shorted my code to better explain my question. This code is placed in a Userform Module. My executed code is fired when I click the command button on my UserForm. My problem is when I click the command button lets say CheckBox 1 & 2 = True. Thus, Var1 = 100, Var2 = 200, Var3 = 300, Var4 = 400 and the Message Box is shown 4 times (Note: UserForm is still open). If I set CheckBox1 = True and CheckBox2 = False and then click my command button a second time I should only see the MsgBox 2 times, right? Wrong, the Message Box is shown 4 times again, because I delared my variables at the top in the Declarations section, which holds the variable value until the Userfrom is Unloaded. So I was wanting to know if there is a way to automatically reset the variables = 0 each time the command button is clicked (Such as Dim var1 As Single = 0). I currently have code that uses the Array and a For Each...Loop to set the variables = 0, which I did not display below. I just figured there may be an easier way. Option Explicit Dim var1 As Single Dim var2 As Single Dim var3 As Single Dim var4 As Single Private Sub CommandButton1_Click() Call UseVariables End Sub Private Sub UseVariables() Dim myVariables As Variant Dim myVar As Variant Call CalculateVariables myVariables = Array(var1, var2, var3, var4) For Each myVar In myVariables If myVar <> 0 Then MsgBox "Do Something" End If Next myVar End Sub Private Sub CalculateVariables() If CheckBox1 = True Then var1 = 100 var2 = 200 End If If CheckBox2 = True Then var3 = 300 var4 = 400 End If End Sub -- Cheers, Ryan "JLGWhiz" wrote: > 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 |
|
||
|
||||
|
Charlie
Guest
Posts: n/a
|
I hate to be the bearer of bad news but this is how I do it (no automatic
reset): If CheckBox1 Then var1 = 100 var2 = 200 Else var1 = 0 var2 = 0 End If "RyanH" wrote: > I realize what you are saying Charlie and JLGWhiz, and I agree. I apologize > for not posting code. I basically shorted my code to better explain my > question. > > This code is placed in a Userform Module. My executed code is fired when I > click the command button on my UserForm. > > My problem is when I click the command button lets say CheckBox 1 & 2 = > True. Thus, Var1 = 100, Var2 = 200, Var3 = 300, Var4 = 400 and the Message > Box is shown 4 times (Note: UserForm is still open). If I set CheckBox1 = > True and CheckBox2 = False and then click my command button a second time I > should only see the MsgBox 2 times, right? Wrong, the Message Box is shown 4 > times again, because I delared my variables at the top in the Declarations > section, which holds the variable value until the Userfrom is Unloaded. > > So I was wanting to know if there is a way to automatically reset the > variables = 0 each time the command button is clicked (Such as Dim var1 As > Single = 0). I currently have code that uses the Array and a For Each...Loop > to set the variables = 0, which I did not display below. I just figured > there may be an easier way. > > Option Explicit > > Dim var1 As Single > Dim var2 As Single > Dim var3 As Single > Dim var4 As Single > > Private Sub CommandButton1_Click() > > Call UseVariables > > End Sub > > Private Sub UseVariables() > > Dim myVariables As Variant > Dim myVar As Variant > > Call CalculateVariables > > myVariables = Array(var1, var2, var3, var4) > > For Each myVar In myVariables > If myVar <> 0 Then > MsgBox "Do Something" > End If > Next myVar > > End Sub > > Private Sub CalculateVariables() > > If CheckBox1 = True Then > var1 = 100 > var2 = 200 > End If > > If CheckBox2 = True Then > var3 = 300 > var4 = 400 > End If > > End Sub > > -- > Cheers, > Ryan > > > "JLGWhiz" wrote: > > > 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 |
|
||
|
||||
|
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
|
Change your CalculateVariable subroutine to the following and the variables
will set themselves to the correct value or reset themselves to 0 depending on the True or False status of the various CheckBoxes... Private Sub CalculateVariables() var1 = -100 * CheckBox1.Value var2 = -200 * CheckBox1.Value var3 = -300 * CheckBox2.Value var4 = -400 * CheckBox2.Value End Sub Note the minus sign in front of the numbers... that is very important. What is going on here is the numerical value is being multiplied by either True or False (depending on if the CheckBox is checked or not) which, in VB, is -1 (minus 1) for True and 0 for False. Hence, using var1 as an example, the multiplication automatically sets the value to -100 (minus 100) times either -1 (minus 1) if the CheckBox is checked (its Value property would be True) or time 0 if it is not checked (its Value property would be False). Since a minus times a minus is a plus, var1 is set to either 100 or 0 automatically. Rick "RyanH" <(E-Mail Removed)> wrote in message news:F1BB5796-6CB9-4683-92DE-(E-Mail Removed)... >I realize what you are saying Charlie and JLGWhiz, and I agree. I >apologize > for not posting code. I basically shorted my code to better explain my > question. > > This code is placed in a Userform Module. My executed code is fired when > I > click the command button on my UserForm. > > My problem is when I click the command button lets say CheckBox 1 & 2 = > True. Thus, Var1 = 100, Var2 = 200, Var3 = 300, Var4 = 400 and the > Message > Box is shown 4 times (Note: UserForm is still open). If I set CheckBox1 = > True and CheckBox2 = False and then click my command button a second time > I > should only see the MsgBox 2 times, right? Wrong, the Message Box is > shown 4 > times again, because I delared my variables at the top in the Declarations > section, which holds the variable value until the Userfrom is Unloaded. > > So I was wanting to know if there is a way to automatically reset the > variables = 0 each time the command button is clicked (Such as Dim var1 As > Single = 0). I currently have code that uses the Array and a For > Each...Loop > to set the variables = 0, which I did not display below. I just figured > there may be an easier way. > > Option Explicit > > Dim var1 As Single > Dim var2 As Single > Dim var3 As Single > Dim var4 As Single > > Private Sub CommandButton1_Click() > > Call UseVariables > > End Sub > > Private Sub UseVariables() > > Dim myVariables As Variant > Dim myVar As Variant > > Call CalculateVariables > > myVariables = Array(var1, var2, var3, var4) > > For Each myVar In myVariables > If myVar <> 0 Then > MsgBox "Do Something" > End If > Next myVar > > End Sub > > Private Sub CalculateVariables() > > If CheckBox1 = True Then > var1 = 100 > var2 = 200 > End If > > If CheckBox2 = True Then > var3 = 300 > var4 = 400 > End If > > End Sub > > -- > Cheers, > Ryan > > > "JLGWhiz" wrote: > >> 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 |
|
||
|
||||
|
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
|
By the way, you can use the same technique on String variable too. For
example... strVar1 = Left("Some text", -9 * CheckBox1.Value) where the 9 is the length of the text in quote marks. Again, note the minus sign in front of the length of the text. This statement will either assign the full string value or the empty string ("") to strVar1 depending on if the CheckBox is checked or not. Rick "Rick Rothstein (MVP - VB)" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)... > Change your CalculateVariable subroutine to the following and the > variables will set themselves to the correct value or reset themselves to > 0 depending on the True or False status of the various CheckBoxes... > > Private Sub CalculateVariables() > > var1 = -100 * CheckBox1.Value > var2 = -200 * CheckBox1.Value > > var3 = -300 * CheckBox2.Value > var4 = -400 * CheckBox2.Value > > End Sub > > Note the minus sign in front of the numbers... that is very important. > What is going on here is the numerical value is being multiplied by either > True or False (depending on if the CheckBox is checked or not) which, in > VB, is -1 (minus 1) for True and 0 for False. Hence, using var1 as an > example, the multiplication automatically sets the value to -100 (minus > 100) times either -1 (minus 1) if the CheckBox is checked (its Value > property would be True) or time 0 if it is not checked (its Value property > would be False). Since a minus times a minus is a plus, var1 is set to > either 100 or 0 automatically. > > Rick > > > "RyanH" <(E-Mail Removed)> wrote in message > news:F1BB5796-6CB9-4683-92DE-(E-Mail Removed)... >>I realize what you are saying Charlie and JLGWhiz, and I agree. I >>apologize >> for not posting code. I basically shorted my code to better explain my >> question. >> >> This code is placed in a Userform Module. My executed code is fired when >> I >> click the command button on my UserForm. >> >> My problem is when I click the command button lets say CheckBox 1 & 2 = >> True. Thus, Var1 = 100, Var2 = 200, Var3 = 300, Var4 = 400 and the >> Message >> Box is shown 4 times (Note: UserForm is still open). If I set CheckBox1 >> = >> True and CheckBox2 = False and then click my command button a second time >> I >> should only see the MsgBox 2 times, right? Wrong, the Message Box is >> shown 4 >> times again, because I delared my variables at the top in the >> Declarations >> section, which holds the variable value until the Userfrom is Unloaded. >> >> So I was wanting to know if there is a way to automatically reset the >> variables = 0 each time the command button is clicked (Such as Dim var1 >> As >> Single = 0). I currently have code that uses the Array and a For >> Each...Loop >> to set the variables = 0, which I did not display below. I just figured >> there may be an easier way. >> >> Option Explicit >> >> Dim var1 As Single >> Dim var2 As Single >> Dim var3 As Single >> Dim var4 As Single >> >> Private Sub CommandButton1_Click() >> >> Call UseVariables >> >> End Sub >> >> Private Sub UseVariables() >> >> Dim myVariables As Variant >> Dim myVar As Variant >> >> Call CalculateVariables >> >> myVariables = Array(var1, var2, var3, var4) >> >> For Each myVar In myVariables >> If myVar <> 0 Then >> MsgBox "Do Something" >> End If >> Next myVar >> >> End Sub >> >> Private Sub CalculateVariables() >> >> If CheckBox1 = True Then >> var1 = 100 >> var2 = 200 >> End If >> >> If CheckBox2 = True Then >> var3 = 300 >> var4 = 400 >> End If >> >> End Sub >> >> -- >> Cheers, >> Ryan >> >> >> "JLGWhiz" wrote: >> >>> 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 > |
|
||
|
||||
|
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
|
One final thought on this method... if you had a lot of variables to set for
any given CheckBox, you could use a With/End With block to avoid having to type in the CheckBox name each time. For example.... With CheckBox1 var1 = -100 * .Value var2 = -200 * .Value etc. End With Rick "Rick Rothstein (MVP - VB)" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)... > Change your CalculateVariable subroutine to the following and the > variables will set themselves to the correct value or reset themselves to > 0 depending on the True or False status of the various CheckBoxes... > > Private Sub CalculateVariables() > > var1 = -100 * CheckBox1.Value > var2 = -200 * CheckBox1.Value > > var3 = -300 * CheckBox2.Value > var4 = -400 * CheckBox2.Value > > End Sub > > Note the minus sign in front of the numbers... that is very important. > What is going on here is the numerical value is being multiplied by either > True or False (depending on if the CheckBox is checked or not) which, in > VB, is -1 (minus 1) for True and 0 for False. Hence, using var1 as an > example, the multiplication automatically sets the value to -100 (minus > 100) times either -1 (minus 1) if the CheckBox is checked (its Value > property would be True) or time 0 if it is not checked (its Value property > would be False). Since a minus times a minus is a plus, var1 is set to > either 100 or 0 automatically. > > Rick > > > "RyanH" <(E-Mail Removed)> wrote in message > news:F1BB5796-6CB9-4683-92DE-(E-Mail Removed)... >>I realize what you are saying Charlie and JLGWhiz, and I agree. I >>apologize >> for not posting code. I basically shorted my code to better explain my >> question. >> >> This code is placed in a Userform Module. My executed code is fired when >> I >> click the command button on my UserForm. >> >> My problem is when I click the command button lets say CheckBox 1 & 2 = >> True. Thus, Var1 = 100, Var2 = 200, Var3 = 300, Var4 = 400 and the >> Message >> Box is shown 4 times (Note: UserForm is still open). If I set CheckBox1 >> = >> True and CheckBox2 = False and then click my command button a second time >> I >> should only see the MsgBox 2 times, right? Wrong, the Message Box is >> shown 4 >> times again, because I delared my variables at the top in the >> Declarations >> section, which holds the variable value until the Userfrom is Unloaded. >> >> So I was wanting to know if there is a way to automatically reset the >> variables = 0 each time the command button is clicked (Such as Dim var1 >> As >> Single = 0). I currently have code that uses the Array and a For >> Each...Loop >> to set the variables = 0, which I did not display below. I just figured >> there may be an easier way. >> >> Option Explicit >> >> Dim var1 As Single >> Dim var2 As Single >> Dim var3 As Single >> Dim var4 As Single >> >> Private Sub CommandButton1_Click() >> >> Call UseVariables >> >> End Sub >> >> Private Sub UseVariables() >> >> Dim myVariables As Variant >> Dim myVar As Variant >> >> Call CalculateVariables >> >> myVariables = Array(var1, var2, var3, var4) >> >> For Each myVar In myVariables >> If myVar <> 0 Then >> MsgBox "Do Something" >> End If >> Next myVar >> >> End Sub >> >> Private Sub CalculateVariables() >> >> If CheckBox1 = True Then >> var1 = 100 >> var2 = 200 >> End If >> >> If CheckBox2 = True Then >> var3 = 300 >> var4 = 400 >> End If >> >> End Sub >> >> -- >> Cheers, >> Ryan >> >> >> "JLGWhiz" wrote: >> >>> 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 > |
|
||
|
||||
|
RyanH
Guest
Posts: n/a
|
Thanks for the help Rick. I actually was using the code I posted as an
example. Your method would help for some of the controls on my Userform but not all. Your method did give me a good idea though to use in the future. -- Cheers, Ryan "Rick Rothstein (MVP - VB)" wrote: > One final thought on this method... if you had a lot of variables to set for > any given CheckBox, you could use a With/End With block to avoid having to > type in the CheckBox name each time. For example.... > > With CheckBox1 > var1 = -100 * .Value > var2 = -200 * .Value > etc. > End With > > Rick > > > "Rick Rothstein (MVP - VB)" <(E-Mail Removed)> wrote in > message news:(E-Mail Removed)... > > Change your CalculateVariable subroutine to the following and the > > variables will set themselves to the correct value or reset themselves to > > 0 depending on the True or False status of the various CheckBoxes... > > > > Private Sub CalculateVariables() > > > > var1 = -100 * CheckBox1.Value > > var2 = -200 * CheckBox1.Value > > > > var3 = -300 * CheckBox2.Value > > var4 = -400 * CheckBox2.Value > > > > End Sub > > > > Note the minus sign in front of the numbers... that is very important. > > What is going on here is the numerical value is being multiplied by either > > True or False (depending on if the CheckBox is checked or not) which, in > > VB, is -1 (minus 1) for True and 0 for False. Hence, using var1 as an > > example, the multiplication automatically sets the value to -100 (minus > > 100) times either -1 (minus 1) if the CheckBox is checked (its Value > > property would be True) or time 0 if it is not checked (its Value property > > would be False). Since a minus times a minus is a plus, var1 is set to > > either 100 or 0 automatically. > > > > Rick > > > > > > "RyanH" <(E-Mail Removed)> wrote in message > > news:F1BB5796-6CB9-4683-92DE-(E-Mail Removed)... > >>I realize what you are saying Charlie and JLGWhiz, and I agree. I > >>apologize > >> for not posting code. I basically shorted my code to better explain my > >> question. > >> > >> This code is placed in a Userform Module. My executed code is fired when > >> I > >> click the command button on my UserForm. > >> > >> My problem is when I click the command button lets say CheckBox 1 & 2 = > >> True. Thus, Var1 = 100, Var2 = 200, Var3 = 300, Var4 = 400 and the > >> Message > >> Box is shown 4 times (Note: UserForm is still open). If I set CheckBox1 > >> = > >> True and CheckBox2 = False and then click my command button a second time > >> I > >> should only see the MsgBox 2 times, right? Wrong, the Message Box is > >> shown 4 > >> times again, because I delared my variables at the top in the > >> Declarations > >> section, which holds the variable value until the Userfrom is Unloaded. > >> > >> So I was wanting to know if there is a way to automatically reset the > >> variables = 0 each time the command button is clicked (Such as Dim var1 > >> As > >> Single = 0). I currently have code that uses the Array and a For > >> Each...Loop > >> to set the variables = 0, which I did not display below. I just figured > >> there may be an easier way. > >> > >> Option Explicit > >> > >> Dim var1 As Single > >> Dim var2 As Single > >> Dim var3 As Single > >> Dim var4 As Single > >> > >> Private Sub CommandButton1_Click() > >> > >> Call UseVariables > >> > >> End Sub > >> > >> Private Sub UseVariables() > >> > >> Dim myVariables As Variant > >> Dim myVar As Variant > >> > >> Call CalculateVariables > >> > >> myVariables = Array(var1, var2, var3, var4) > >> > >> For Each myVar In myVariables > >> If myVar <> 0 Then > >> MsgBox "Do Something" > >> End If > >> Next myVar > >> > >> End Sub > >> > >> Private Sub CalculateVariables() > >> > >> If CheckBox1 = True Then > >> var1 = 100 > >> var2 = 200 > >> End If > >> > >> If CheckBox2 = True Then > >> var3 = 300 > >> var4 = 400 > >> End If > >> > >> End Sub > >> > >> -- > >> Cheers, > >> Ryan > >> > >> > >> "JLGWhiz" wrote: > >> > >>> 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 > > > > |
|
||
|
||||
|
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
|
What are the controls you have that it would not be helpful for (there is
probably another way to do it for them)? Note: It's very hard to give you advice for something you don't tell us about.<g> Rick "RyanH" <(E-Mail Removed)> wrote in message news:8A1E6BE6-AED6-4847-A256-(E-Mail Removed)... > Thanks for the help Rick. I actually was using the code I posted as an > example. Your method would help for some of the controls on my Userform > but > not all. Your method did give me a good idea though to use in the future. > > > -- > Cheers, > Ryan > > > "Rick Rothstein (MVP - VB)" wrote: > >> One final thought on this method... if you had a lot of variables to set >> for >> any given CheckBox, you could use a With/End With block to avoid having >> to >> type in the CheckBox name each time. For example.... >> >> With CheckBox1 >> var1 = -100 * .Value >> var2 = -200 * .Value >> etc. >> End With >> >> Rick >> >> >> "Rick Rothstein (MVP - VB)" <(E-Mail Removed)> wrote >> in >> message news:(E-Mail Removed)... >> > Change your CalculateVariable subroutine to the following and the >> > variables will set themselves to the correct value or reset themselves >> > to >> > 0 depending on the True or False status of the various CheckBoxes... >> > >> > Private Sub CalculateVariables() >> > >> > var1 = -100 * CheckBox1.Value >> > var2 = -200 * CheckBox1.Value >> > >> > var3 = -300 * CheckBox2.Value >> > var4 = -400 * CheckBox2.Value >> > >> > End Sub >> > >> > Note the minus sign in front of the numbers... that is very important. >> > What is going on here is the numerical value is being multiplied by >> > either >> > True or False (depending on if the CheckBox is checked or not) which, >> > in >> > VB, is -1 (minus 1) for True and 0 for False. Hence, using var1 as an >> > example, the multiplication automatically sets the value to -100 (minus >> > 100) times either -1 (minus 1) if the CheckBox is checked (its Value >> > property would be True) or time 0 if it is not checked (its Value >> > property >> > would be False). Since a minus times a minus is a plus, var1 is set to >> > either 100 or 0 automatically. >> > >> > Rick >> > >> > >> > "RyanH" <(E-Mail Removed)> wrote in message >> > news:F1BB5796-6CB9-4683-92DE-(E-Mail Removed)... >> >>I realize what you are saying Charlie and JLGWhiz, and I agree. I >> >>apologize >> >> for not posting code. I basically shorted my code to better explain >> >> my >> >> question. >> >> >> >> This code is placed in a Userform Module. My executed code is fired >> >> when >> >> I >> >> click the command button on my UserForm. >> >> >> >> My problem is when I click the command button lets say CheckBox 1 & 2 >> >> = >> >> True. Thus, Var1 = 100, Var2 = 200, Var3 = 300, Var4 = 400 and the >> >> Message >> >> Box is shown 4 times (Note: UserForm is still open). If I set >> >> CheckBox1 >> >> = >> >> True and CheckBox2 = False and then click my command button a second >> >> time >> >> I >> >> should only see the MsgBox 2 times, right? Wrong, the Message Box is >> >> shown 4 >> >> times again, because I delared my variables at the top in the >> >> Declarations >> >> section, which holds the variable value until the Userfrom is >> >> Unloaded. >> >> >> >> So I was wanting to know if there is a way to automatically reset the >> >> variables = 0 each time the command button is clicked (Such as Dim >> >> var1 >> >> As >> >> Single = 0). I currently have code that uses the Array and a For >> >> Each...Loop >> >> to set the variables = 0, which I did not display below. I just >> >> figured >> >> there may be an easier way. >> >> >> >> Option Explicit >> >> >> >> Dim var1 As Single >> >> Dim var2 As Single >> >> Dim var3 As Single >> >> Dim var4 As Single >> >> >> >> Private Sub CommandButton1_Click() >> >> >> >> Call UseVariables >> >> >> >> End Sub >> >> >> >> Private Sub UseVariables() >> >> >> >> Dim myVariables As Variant >> >> Dim myVar As Variant >> >> >> >> Call CalculateVariables >> >> >> >> myVariables = Array(var1, var2, var3, var4) >> >> >> >> For Each myVar In myVariables >> >> If myVar <> 0 Then >> >> MsgBox "Do Something" >> >> End If >> >> Next myVar >> >> >> >> End Sub >> >> >> >> Private Sub CalculateVariables() >> >> >> >> If CheckBox1 = True Then >> >> var1 = 100 >> >> var2 = 200 >> >> End If >> >> >> >> If CheckBox2 = True Then >> >> var3 = 300 >> >> var4 = 400 >> >> End If >> >> >> >> End Sub >> >> >> >> -- >> >> Cheers, >> >> Ryan >> >> >> >> >> >> "JLGWhiz" wrote: >> >> >> >>> 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 >> > >> >> |
|
||
|
||||
|
|
|
| |
![]() |
| Thread Tools | |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Runtime Error 91 Object variable or With block variable not set. | Tim | Microsoft Excel Programming | 9 | 5th Jun 2008 10:03 PM |
| Change variable values at runtime in debug mode? | =?Utf-8?B?VmFyYWQ=?= | Microsoft Dot NET | 2 | 25th Feb 2005 09:53 PM |
| Runtime Error 91: Object variable or width block variable not set | BYang | Windows XP Internet Explorer | 1 | 4th May 2004 02:39 PM |
| Runtime error 91, Object variable not set or with block variable | Sandeep Prasad | Windows XP Internet Explorer | 0 | 30th Mar 2004 04:28 AM |
| runtime error 91: object variable or with block variable not set | Mark | Windows XP General | 1 | 21st Jan 2004 02:21 AM |
Powered by vBulletin®. Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2010, Crawlability, Inc. |




