如何根据用户窗体组合框响应动态计算分数

时间:2019-04-30 13:36:52

标签: excel vba userform

我有一个包含7个组合框的用户窗体,这些组合框可以选择YesNoPartiallyN/A。根据每个框的响应,我有一个文本框填充总分。

Yes = 1
Partially = 0.5
No = 0
NA = 0

我有基于组合框响应进行计算的代码,如果我简单地除以总框数(7),则计算得出,但是并非所有表单都会有7个响应(NA是一个选项,但基本上不计入或反对他们)。因此,我需要弄清楚如何将总分除以总答复。我敢肯定这是超级容易的,但是我并没有努力弄清楚。

这是我当前在其中提供的代码,无法提供正确的%

TXTScore = Format((nYes + nPartial * 0.5) / nYes + nPartial + nNo, "Percent")
Private Sub CommandButton1_Click()

Dim c As Control, nYes As Long, nPartial As Long, nNo As Long


For Each c In Me.Controls
    If TypeName(c) = "ComboBox" Then
        If c.Value = "Yes" Then nYes = nYes + 1
        If c.Value = "Partially" Then nPartial = nPartial + 1
        If c.Value = "No" Then nNo = nNo + 1
    End If
Next c

TXTScore = Format((nYes + nPartial * 0.5) / nYes + nPartial + nNo, "Percent")
End Sub

例如-回答6是,则1 NA = 100%,回答5是,部分1和1 NA等于92%

1 个答案:

答案 0 :(得分:1)

您也应该算NA(我想加上一些括号)

Private Sub CommandButton1_Click()

Dim c As Control, nYes As Long, nPartial As Long, nNo As Long, nNA As Long
nYes = 0
nPartial = 0
nNo = 0
nNA = 0


For Each c In Me.Controls
    If TypeName(c) = "ComboBox" Then
        If c.Value = "Yes" Then nYes = nYes + 1
        If c.Value = "Partially" Then nPartial = nPartial + 1
        If c.Value = "No" Then nNo = nNo + 1
        If c.Value = "NA" Then nNA = nNA + 1
    End If
Next c

TXTScore = Format((nYes + nPartial * 0.5) / (nYes + nPartial + nNo + nNA), "Percent")
End Sub