Home > Software engineering >  issue with vba count
issue with vba count

Time:12-23

hi everyone i am having problem with the vba. I Want the division box to count the number of txt boxes is been filled for example if txtbox1 > 0 then txt.divide = txt.divide 1 similarly if other text boxes is been used then it will keep adding 1 like if there 2 text boxes used then division value will be 2 etc

any can help?

If Trading_calculator1.txt_currency1.Value > 0 Then
  Trading_calculator1.txt_divide.Value = txt_divide   1 
ElseIf Trading_calculator1.txt_currency2.Value > 0 Then 
  Trading_calculator1.txt_divide.Value = txt_divide   1 
End If

CodePudding user response:

To test if a TextBox is empty, you can do Len(TextBox.Text) > 0. This returns TRUE when there is any text in the textbox.

You can make this dynamic by using a loop to search every control in the userform (Userform.Controls collection). This way you don't need an IF statement for every textbox individually. You can use the TypeName Function to test if the control is a "TextBox" and then check if it is not empty.

    Dim TextBoxCount As Long, ctrl as Variant
    For Each ctrl In Trading_calculator1.Controls
        If TypeName(ctrl) = "TextBox" Then
            If Len(ctrl.Text) > 0 Then TextBoxCount = TextBoxCount   1
        End If
    Next
    txt_divide = TextBoxCount

CodePudding user response:

I solved it here's what I used:

Dim divide As Integer
divide = 0

If Trading_calculator1.txt_currency1.Value > 0 Then divide = divide   1
If Trading_calculator1.txt_currency1.Value = "" Then divide = divide - 1

Trading_calculator1.txt_divide = divide

it worked for me the first if statement will add one if the text box value change the 2nd if will change it back if there is no value and return it in 0

  • Related