Counter in userform, part 2

wpryan

Well-known Member
Joined
May 26, 2009
Messages
534
Office Version
  1. 365
Platform
  1. Windows
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:
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:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top