计数TextBox填充(仅限部分)

时间:2018-03-19 18:34:50

标签: vba textbox userform

我正在尝试计算7个文本框,如果它们被填充我将为每个值乘以10.50的值,但我无法进行此验证...我在下面编写了代码但是它没有使用

  

错误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

1 个答案:

答案 0 :(得分:0)

如果我了解您的要求,您需要添加文本框中包含的值,然后乘以10.5。如果这是正确的,请尝试以下代码:

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