VBA USER FORM错误6可以解决

时间:2017-10-20 12:10:44

标签: excel vba excel-vba

我目前有一堆组合框,可以选择是,否或不适用 然后我有一个运行计算的命令按钮然后移动到下一页

Private Sub CommandButton2_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)
OUTBX1.Text = Format(a / b, "00.00%")
MultiPage1.Value = 1
End Sub

现在我遇到的唯一问题是,如果所有方框都被检查为“N / A”,那么我得到0/0问题(错误6:溢出)

我知道使用Excel我会使用ERRORIF公式来解决这个问题。有没有办法告诉VBA,如果:

=ERRORIF (b=0) then MultiPage1.Value = 1 else 
    OUTBX1.Text = Format(a / b, "00.00%")

1 个答案:

答案 0 :(得分:0)

感谢大家的帮助,对此的最终答案编码如下:

Private Sub CommandButton2_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)

If b = 0 Then
MultiPage1.Value = 1

Else
OUTBX1.Text = Format(a / b, "00.00%")
MultiPage1.Value = 1
End If
End Sub