Excel VBA: Stopping a section of code (correctly)

2019-08-29 12:44发布

Wanting to check this Exit Sub at the top isn't going to cause me other problems. It seems to work, added to rest of my code for reference.

Everything from 'Establish File Extension type has worked to date/to this point. Unsure if just showing the below segment of code is sufficient to answer the question.

    If invalid > 0 Then mbResult = MsgBox("Something's missing. Please check and try again. There are " & invalid & " incomplete fields.", _
    vbOKOnly)

    Select Case mbResult
    Case vbOK
        Exit Sub
    End Select

    'Establish File Extension type
    With Destwb
        If Sourcewb.Name = .Name Then
            With Application
                .ScreenUpdating = True
                .EnableEvents = True
            End With
        End If
    End With

    FileExtStr = ".xlsm"
    FileFormatNum = 52

    'Save the new workbook and close it
    TempFilePath = ("www.mysharepoint.com") & "\"
    TempFileName = Range("A1").Text

    'Confirm Submission
    mbResult = MsgBox("This submission cannot be undone. Would you like to continue?", _
    vbYesNo)

    Select Case mbResult
    Case vbNo
        Exit Sub
    End Select

    'Build .SaveAs file Name based on variables established previously
    With Destwb
        .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
    End With

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With

    Application.DisplayAlerts = True
    ThisWorkbook.Activate

    'Display successful submission message
    MsgBox ("Thank you, your assessment has been successfully submitted.")

    ActiveSheet.Protect
End Sub

1条回答
Bombasti
2楼-- · 2019-08-29 13:29

You can change these lines:

    If invalid > 0 Then mbResult = MsgBox("Something's missing. Please check and try again. There are " & invalid & " incomplete fields.", _
    vbOKOnly)

    Select Case mbResult
    Case vbOK
        Exit Sub
    End Selec

to this:

    If invalid > 0 Then
        MsgBox "Something's missing. Please check and try again. There are " & _
                invalid & " incomplete fields.", vbOKOnly
        Exit Sub
    End If
查看更多
登录 后发表回答