错误消息框select语句处理是或否

时间:2016-03-17 02:37:05

标签: vba excel-vba excel

需要有关错误消息的yes no部分的帮助,如果是,我希望再次启动代码,如果没有则退出sub。

Public Sub Reset()
    Dim pt As PivotTable
    Dim slice As Slicer

    Application.ScreenUpdating = False

    ActiveWorkbook.Model.Refresh

    For Each pt In ActiveSheet.PivotTables
        pt.RefreshTable
        For Each slice In pt.Slicers
           slice.SlicerCache.ClearAllFilters
On Error GoTo 0
    Next slice
        pt.PivotCache.Refresh
    Next pt
Error 0:
    MsgBox "Sorry, Missing data, do you wish to continue?", _
                vbCritical vbYesNo, "Restart process!"
            Select Case vbYesNo
                Case yes
                    MergeMultipleSheets
                Case Else
                Exit Sub
            End Select
    Application.ScreenUpdating = True
End Sub

2 个答案:

答案 0 :(得分:1)

以下是您的代码的任何部分(假设您想要启动模块MergeMultipleSheets,如果单击是按钮):

Sub Reset()
    Dim xlAns As Integer

    xlAns = MsgBox("Sorry, Missing data, do you wish to continue?", vbYesNo, "Restart process!")
    Select Case xlAns
    Case vbYes
'        do something
'        if You want to call sub MergeMultipleSheets
        MergeMultipleSheets
    Case Else
'        do something
        Exit Sub
    End Select
End Sub

答案 1 :(得分:0)

快速示例

Sub MsgBx()
    Dim xlMsgBox As Integer
    Dim Cancel As Boolean

    xlMsgBox = MsgBox("Do you want to do this ", vbYesNoCancel)

    If xlMsgBox = vbCancel Then
        Cancel = True ' Exit
        Exit Sub
    ElseIf xlMsgBox = vbYes Then
'          do something
    ElseIf xlMsgBox = vbNo Then
'          do something
    End If

End Sub