如何计算 VBA 用户表单中多个组合框的百分比

how to calculate a percentage with multiple comboboxes in VBA User form

您好,我需要在 VBA 的用户表单中给出多页的百分比分数。 如下所示

我已将组合框格式化如下:

 Private Sub UserForm_Initialize()
 With Cbx1_1
.AddItem "N/A"
.AddItem "Yes"
.AddItem "No"
 End With

With Cbx1_2
.AddItem "N/A"
.AddItem "Yes"
.AddItem "No"
End With

With Cbx1_3
.AddItem "N/A"
.AddItem "Yes"
.AddItem "No"
End With

With Cbx1_4
.AddItem "N/A"
.AddItem "Yes"
.AddItem "No"
 End With
 OUTBX.Value = " "
 End Sub

回答完问题后问题来了,我希望包含一个名为 "Calculate" 的按钮,它会导致 "ResultBx" 中的百分比等于 67%。

这个想法是计算看起来像这样:

 ResultBx.Value=(COUNTIF(Range(Cbx1_1,Cbx1_2,Cbx1_3,Cbx1_4),"=Yes"))/
 ((COUNTA(Range(Cbx1_1,Cbx1_2,Cbx1_3,Cbx1_4)))-(COUNTIF((Range 
 (Cbx1_1,Cbx1_2,Cbx1_3,Cbx1_4)),"=N/A")))

我知道我在此处包括 Excel 代码,但我需要 "convert" 将其 VBA

有人可以帮忙吗?

假设您的按钮名为 CommandButton1:

Private Sub CommandButton1_Click()
Dim a As Long, b As Long

a = IIf(cbx1_1.Value = "Yes", 1, 0) + IIf(cbx1_2.Value = "Yes", 1, 0) + IIf(cbx1_3.Value = "Yes", 1, 0) + IIf(cbx1_4.Value = "Yes", 1, 0)
b = 4 - IIf(cbx1_1.Value = "N/A", 1, 0) - IIf(cbx1_2.Value = "N/A", 1, 0) - IIf(cbx1_3.Value = "N/A", 1, 0) - IIf(cbx1_4.Value = "N/A", 1, 0)
OUTBX.Text = Format(a / b, "00%")

End Sub

或者如果您愿意:

Private Sub CommandButton1_Click()

OUTBX.Text = Format( _
    ( _
    IIf(cbx1_1.Value = "Yes", 1, 0) + _
    IIf(cbx1_2.Value = "Yes", 1, 0) + _
    IIf(cbx1_3.Value = "Yes", 1, 0) + _
    IIf(cbx1_4.Value = "Yes", 1, 0) _
    ) / _
    (4 - _
    IIf(cbx1_1.Value = "N/A", 1, 0) - _
    IIf(cbx1_2.Value = "N/A", 1, 0) - _
    IIf(cbx1_3.Value = "N/A", 1, 0) - _
    IIf(cbx1_4.Value = "N/A", 1, 0) _
    ), "00%")

End Sub