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

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

问题描述:

您好,我需要为VBA中的用户窗体中的多页提供百分比分数。 如下图所示 enter image description here如何计算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 

回答问题后,现在问题来了,我期待有一个名为“计算”按钮,这将导致“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中的代码,但我需要“转换”它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