Search code examples
vbatextboxuserform

Count TextBox Filled (Only Some)


I am trying to count 7 textboxes if they are filled I am going to multiply for each one the value of 10.50, but I am not able to do this verification ... I made the code below but it is not working with the

error 91

Sub btnCal_Click()

    Dim cont As Integer
    Dim ctrl As Control

    cont = 1

    For Each ctrl In Me.Controls
        If TypeOf ctrl Is TextBox Then
            If Not IsNull(ctrl.Value) And Left(ctrl.Name, 6) = "txtDp" & cont Then
                cont = cont + 1
            End If
        End If
    Next ctrl

    txtEx.Value = ctrl * 10.5

End Sub

Solution

  • If I understand your requirements, you need to add the values contained in the textboxes and then multiply by 10.5. If this is correct, then try the following code:

    Private Sub btnCal_Click()
       Dim cont As Integer
       Dim ctrl As Control
    
       cont = 0
    
       For Each ctrl In Me.Controls
          If TypeOf ctrl Is TextBox Then
             If Val(ctrl.Text) <> 0 And Left(ctrl.Name, 5) = "txtDp" Then
                cont = cont + Val(ctrl.Text)
             End If
          End If
       Next ctrl
    
       txtEx.Text = cont * 10.5
    End Sub