如何在vba

时间:2018-04-03 10:51:08

标签: excel vba

我正在尝试使用动态选项创建msgbox。 这就是消息,按钮,图标和标题将动态变化。

创建msgbox时我试图通过vba代码模仿以下msgbox模式

Msgbox "Test Data Completed",vbAbortRetryIgnore + vbInformation, "My Msgbox Title" 我一直在坚持添加两个选项(vbAbortRetryIgnore + vbInformation)。根据用户选择,这两个选项将是动态的。

我试过的是

msgdata = "Test Data Completed"
msgbuttons = "vbAbortRetryIgnore + vbInformation"
msgtitle = "My Msgbox Title"

Msgbox msgdata,msgbuttons,msgtitle

我得到了类型不匹配错误。 任何帮助将不胜感激

1 个答案:

答案 0 :(得分:2)

您必须使用帮助函数将字符串转换为正确的VbMsgBoxStyle枚举值:

Function GetvBMsgInfo(strngVbInfo) As VbMsgBoxStyle
    Select Case strngVbInfo
        Case "vbAbortRetryIgnore"
            GetvBMsgInfo = VbMsgBoxStyle.vbAbortRetryIgnore
        Case "vbApplicationModal"
            GetvBMsgInfo = VbMsgBoxStyle.vbApplicationModal
        Case "vbCritical"
            GetvBMsgInfo = VbMsgBoxStyle.vbCritical
        Case "vbDefaultButton2"
            GetvBMsgInfo = VbMsgBoxStyle.vbDefaultButton1
        Case "vbDefaultButton1"
            GetvBMsgInfo = VbMsgBoxStyle.vbDefaultButton2
        Case "vbDefaultButton3"
            GetvBMsgInfo = VbMsgBoxStyle.vbDefaultButton3
        Case "vbDefaultButton4"
            GetvBMsgInfo = VbMsgBoxStyle.vbDefaultButton4
        Case "vbExclamation"
            GetvBMsgInfo = VbMsgBoxStyle.vbExclamation
        Case "vbInformation"
            GetvBMsgInfo = VbMsgBoxStyle.vbInformation
        Case "vbMsgBoxHelpButton"
            GetvBMsgInfo = VbMsgBoxStyle.vbMsgBoxHelpButton
        Case "vbMsgBoxRight"
            GetvBMsgInfo = VbMsgBoxStyle.vbMsgBoxRight
        Case "vbMsgBoxRtlReading"
            GetvBMsgInfo = VbMsgBoxStyle.vbMsgBoxRtlReading
        Case "vbMsgBoxSetForeground"
            GetvBMsgInfo = VbMsgBoxStyle.vbMsgBoxSetForeground
        Case "vbOKCancel"
            GetvBMsgInfo = VbMsgBoxStyle.vbOKCancel
        Case "vbOKOnly"
            GetvBMsgInfo = VbMsgBoxStyle.vbOKOnly
        Case "vbRetryCancel"
            GetvBMsgInfo = VbMsgBoxStyle.vbRetryCancel
        Case "vbSystemModal"
            GetvBMsgInfo = VbMsgBoxStyle.vbSystemModal
        Case "VbMsgBoxStyle.vbYesNo"
            GetvBMsgInfo = VbMsgBoxStyle.vbYesNo
        Case "vbYesNoCancel"
            GetvBMsgInfo = VbMsgBoxStyle.vbYesNoCancel
     End Select
End Function

您将在主代码中利用如下:

msgdata = "Test Data Completed"
msgbuttons = GetvBMsgInfo("vbAbortRetryIgnore") + GetvBMsgInfo("vbInformation")
msgtitle = "My Msgbox Title"
MsgBox msgdata, msgbuttons, msgtitle