Hi All, this is similar to a previously answered thread: Counter in Userform
This time however, I need to be able to store the variable on the project level. That is, I need it to be available after the sub runs. The scenario is like this:
User enters data in texboxes 1, 2, 3 and 4, and then presses a command button. Some calculations are made, and the results are displayed on the userform.
Textboxes 1 and 2 are emptied, and textboxes 3 and 4 are disabled.
The command button is disabled.
The user will take the calculated data and do something with it (outside of Excel)
The user will then enter new values for textboxes 1, 2, and enter values for textboxes 5, 6, 7 and 8.
I would like that the command button be re-enabled after new values in texboxes 1 and 2 are populated, and textboxes 5, 6, 7 and 8 are populated.
I added a counter that increments by 1 when the command button is entered, and entered a condition in the "after update" code of the textboxes that are looking to see if the textboxes are all populated. The problem is that after the sub routine for pressing the command button is done, the counter variable is lost.
Here is the code for the command button:
Here is the code of one of the textboxes:
...any help is appreciated...
This time however, I need to be able to store the variable on the project level. That is, I need it to be available after the sub runs. The scenario is like this:
User enters data in texboxes 1, 2, 3 and 4, and then presses a command button. Some calculations are made, and the results are displayed on the userform.
Textboxes 1 and 2 are emptied, and textboxes 3 and 4 are disabled.
The command button is disabled.
The user will take the calculated data and do something with it (outside of Excel)
The user will then enter new values for textboxes 1, 2, and enter values for textboxes 5, 6, 7 and 8.
I would like that the command button be re-enabled after new values in texboxes 1 and 2 are populated, and textboxes 5, 6, 7 and 8 are populated.
I added a counter that increments by 1 when the command button is entered, and entered a condition in the "after update" code of the textboxes that are looking to see if the textboxes are all populated. The problem is that after the sub routine for pressing the command button is done, the counter variable is lost.
Here is the code for the command button:
VBA Code:
Private Sub cmd_Calculate_PS5_v1_Click()
Dim counter As Integer
counter = 0
If txt_Power_v1.Value = "" Or txt_PS5a_v1_mW_LasON.Value = "" Or txt_PS5b_v1_mW_LasON.Value = "" _
Or txt_PS5a_v1_CF_old.Value = "" Or txt_PS5b_v1_CF_old.Value = "" Then
MsgBox "Please enter all required data" _
& vbCr & "(Power Meter, Displayed Power PS5a&b [mW] and PS5a&b CF [old])", vbInformation, "Enter required data"
Exit Sub
End If
txt_PS5a_v1_CF_new.Value = Round((txt_Power_v1.Value / txt_PS5a_v1_mW_LasON.Value) * txt_PS5a_v1_CF_old.Value, 0)
txt_PS5b_v1_CF_new.Value = Round((txt_Power_v1.Value / txt_PS5b_v1_mW_LasON.Value) * txt_PS5b_v1_CF_old.Value, 0)
With txt_PS4a_v1_mW_LasON
.Enabled = True
.BackColor = &H80000005
End With
With txt_PS4b_v1_mW_LasON
.Enabled = True
.BackColor = &H80000005
End With
With txt_PS4a_v1_CF_old
.Enabled = True
.BackColor = &H80000005
End With
With txt_PS4b_v1_CF_old
.Enabled = True
.BackColor = &H80000005
End With
cmd_Calculate_Offsets.Enabled = False
cmd_Calculate_PS5_v1.Enabled = False
cmd_Calulate_PS4_v1.Enabled = True
txt_PS5a_v1_mW_LasON.Value = ""
txt_PS5b_v1_mW_LasON.Value = ""
With txt_PS5a_v1_CF_old
.Enabled = False
.BackColor = &H80000004
End With
With txt_PS5b_v1_CF_old
.Enabled = False
.BackColor = &H80000004
End With
MsgBox "Please enter the calculated PS5a&b values into Service View before calibrating PS4!", vbInformation, "Reminder"
counter = counter +1
End Sub
Here is the code of one of the textboxes:
VBA Code:
Private Sub txt_PS5b_v1_mW_LasON_Change()
If (txt_PS5a_v1_mW_LasON.Value <> "" And txt_PS5b_v1_mW_LasON.Value <> "" And txt_PS5a_v1_CF_old.Value <> "" And txt_PS5b_v1_CF_old.Value <> "") And counter < 1 Then
cmd_Calculate_PS5_v1.Enabled = True
End If
End Sub
...any help is appreciated...
Last edited: