IF语句使用vba中的消息框显示复选框的结果

时间:2018-12-23 00:23:15

标签: excel vba excel-vba

我对VBA还是很陌生,并且创建了一个用户表格,该表格允许用户使用多个复选框选择他们希望在一个学年的第一学期和第二学期修读哪些科目。一旦用户选择了他们的选项,消息框就会显示他们选择的内容的摘要。目前,我的代码有效,但仅显示每个学期的第一个选择项,我需要在下面的代码中进行哪些更改,以便显示他们选择的所有选项?从下面的屏幕截图中,您可以看到我的表单,并且您可以看到消息框仅显示每个列表中的第一个选项。非常感谢!

Screenshot

'确认选项

If Me.IB1.Value = True Then
msg1 = ("International Business 1" & Chr(13))
End If

If Me.BP1.Value = True Then
msg1 = msg1 + ("Business Programming 1" & Chr(13))
End If

If Me.BP.Value = True Then
msg1 = msg1 + ("Business Planning" & Chr(13))
End If

If Me.LI.Value = True Then
msg1 = msg1 + ("Leading & Influencing" & Chr(13))
End If

If Me.MS.Value = True Then
msg1 = msg1 + ("Management Science" & Chr(13))
End If

If Me.CS.Value = True Then
msg2 = ("Corporate Strategy" & Chr(13))
End If

If Me.PM.Value = True Then
msg2 = msg2 + ("Project Management" & Chr(13))
End If

If Me.BE.Value = True Then
msg2 = msg2 + ("Business Ethics" & Chr(13))
End If

If Me.BF.Value = True Then
msg2 = msg2 + ("Business Finance" & Chr(13))
End If

If Me.IB2.Value = True Then
msg2 = msg2 + ("International Business 2" & Chr(13))
End If

If Me.BP2.Value = True Then
msg2 = msg2 + ("Business Programming 2" & Chr(13))
End If

If Me.CG.Value = True Then
msg2 = msg2 + ("Corporate Governance" & Chr(13))
End If

MsgBox ("You have chosen the following options for semester 1" & Chr(13) & msg1 & _
"You have chosen the following options for semester 2" & Chr(13) & msg2)
End If

1 个答案:

答案 0 :(得分:1)

尝试关注

Private Sub btnConfirm_Click()
Dim strResult As String

    strResult = ""
    If Me.IB.Value = True Then
        strResult = strResult & Me.IB.Caption
    End If

    If Me.BP.Value = True Then
        strResult = strResult & vbCrLf & Me.BP.Caption
    End If

    If Me.BP1.Value = True Then
        strResult = strResult & vbCrLf & Me.BP1.Caption
    End If

    If Me.LI.Value = True Then
        strResult = strResult & vbCrLf & Me.LI.Caption
    End If

    MsgBox strResult
End Sub
  

编辑

如果要迭代用户表单中的所有复选框,请尝试执行以下操作

Private Sub btnShowSelected_Click()
Dim strResult As String

strResult = "You have selected following checkboxes." & vbCrLf
For Each ctrl In Me.Controls
    If TypeName(ctrl) = "CheckBox" Then
        If ctrl.Value = True Then
            strResult = strResult & vbCrLf & ctrl.Caption
        End If
    End If
Next ctrl

MsgBox strResult
End Sub

enter image description here