UserForm内部计数器用于计算

时间:2015-12-16 20:32:38

标签: excel vba excel-vba userform

我目前正在尝试创建一个检查userform文本框值的机制(如果它是空的,忽略;如果是文本,msgbox错误;如果是数字然后继续),以便从三个不同的文本框计算GPA值。有人可以为三个不同的学校输入三个不同的盒子,但是可能只有一个可以输入,也可能只有两个或三个。我认为以下内容可行,但当我输入任何值时,它会一直触发错误:

  

"运行时错误' 6':溢出"。

有没有人知道这怎么可行?

Private Sub TextBox14_Change()
   GPASum
End Sub

Private Sub TextBox18_Change()
   GPASum
End Sub

Private Sub TextBox23_Change()
   GPASum
End Sub

Private Sub GPASum()

   Dim Total As Double
   Dim Count As Double
   Total = 0
   Count = 0

   If Len(TextBox14.Value) > 0 Then
      If IsNumeric(TextBox14.Value) = False Then
         MsgBox "Error, only numbers allowed"
         TextBox14.Value = Null
      Else: Total = Total + CDbl(TextBox14.Value): Count = Count + 1
      End If
   End If

   If Len(TextBox18.Value) > 0 Then
      If IsNumeric(TextBox18.Value) = False Then
         MsgBox "Error, only numbers allowed"
         TextBox18.Value = Null
      Else: Total = Total + CDbl(TextBox18.Value): Count = Count + 1
      End If 
   End If

   If Len(TextBox23.Value) > 0 Then
      If IsNumeric(TextBox23.Value) = False Then
         MsgBox "Error, only numbers allowed"
         TextBox23.Value = Null
      Else: Total = Total + CDbl(TextBox23.Value): Count = Count + 1
      End If
   End If

   TextBox27.Value = Total / Count
End Sub

2 个答案:

答案 0 :(得分:0)

最好将大部分逻辑移至单独的Sub并为每个文本框调用

测试:

Private Sub GPASum()

    Dim Total As Double
    Dim count As Long, numErrs As Long
    Total = 0
    count = 0
    numErrs = 0

    DoAdd TextBox14, Total, count, numErrs
    DoAdd TextBox18, Total, count, numErrs
    DoAdd TextBox23, Total, count, numErrs

    If numErrs > 0 Then
        MsgBox numErrs & " non-numeric value(s) cleared", vbExclamation, _
                    "Numbers only please!"
    End If

    'EDIT:
    If count > 0 Then
        TextBox27.Value = Total / count
    Else
        TextBox27.Value = Null
    End If

End Sub

Sub DoAdd(tb As Object, ByRef tot As Double, numVals As Long, ByRef numErrs As Long)
    Dim v
    v = tb.Value
    If Len(v) > 0 Then
        If Not IsNumeric(v) Then
            tb.Value = Null
            numErrs = numErrs + 1
        Else
            tot = tot + CDbl(v)
            numVals = numVals + 1
        End If
    End If
End Sub

答案 1 :(得分:0)

非常感谢大家的帮助。我最终使用了@ScottCraner的建议,最后得到了这个有效的代码:

Private Sub GPASum()
Dim Total As Double
Dim count As Double
Total = 0
count = 0

If Len(TextBox14.Value) > 0 Then
If IsNumeric(TextBox14.Value) = False Then
MsgBox "Error, only numbers allowed"
TextBox14.Value = 0
Else: Total = Total + CDbl(TextBox14.Value): count = count + 1
End If
Else:
End If

If Len(TextBox18.Value) > 0 Then
If IsNumeric(TextBox18.Value) = False Then
MsgBox "Error, only numbers allowed"
TextBox18.Value = 0
Exit Sub
Else: Total = Total + CDbl(TextBox18.Value): count = count + 1
End If
Else:
End If

If Len(TextBox23.Value) > 0 Then
If IsNumeric(TextBox23.Value) = False Then
MsgBox "Error, only numbers allowed"
TextBox23.Value = 0
Exit Sub
Else: Total = Total + CDbl(TextBox23.Value): count = count + 1
End If
Else:
End If

TextBox27.Value = Total / count
End Sub

Private Sub TextBox14_Change()
GPASum
End Sub

Private Sub TextBox18_Change()
GPASum
End Sub

Private Sub TextBox23_Change()
GPASum
End Sub