+ Reply to Thread
Results 1 to 3 of 3

MsgBox with VBYesNoCancel

Hybrid View

  1. #1
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,876

    MsgBox with VBYesNoCancel

    Hey!!

    If suppose I write a code where at the time of closing the workbook a message box appears asking me if I wanted to make a back up of this file.

    Clicking Yes would Make a back Up
    Clicking No would Exit Workbook
    And Clicking cancel would just cancel the Msg Box.

    How should I write the code for the cancel button.

    I have already written codes for the yes & no buttons.

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        Dim Msg As String
        Dim Ans As Variant
        Dim FileName As String
        Msg = ("Would you like to make a Back-Up of this file??")
        Ans = MsgBox(Msg, vbYesNo)
        If Ans = vbYes Then
            FileName = "C:\Users\J\Desktop\Back Up " & ThisWorkbook.Name
            ThisWorkbook.SaveCopyAs FileName
        End If
        If Ans = vbNo Then
            MsgBox "No Problemo!!"
        End If
    End Sub
    Cheers!
    Deep Dave

  2. #2
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: MsgBox with VBYesNoCancel

    Selecting cancel prevents the workbook from closing
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        Dim Msg As String
        Dim Ans As Long
        Dim FileName As String
        Msg = "Would you like to make a Back-Up of this file??"
        Ans = MsgBox(Msg, vbYesNoCancel)
        Select Case Ans
        Case vbYes
            FileName = "C:\Users\J\Desktop\Back Up " & ThisWorkbook.Name
            ThisWorkbook.SaveCopyAs FileName
        Case vbNo
            MsgBox "No Problemo!!"
        Case vbCancel
            Cancel = True
        End Select
    End Sub
    Elegant Simplicity............. Not Always

  3. #3
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,876

    Re: MsgBox with VBYesNoCancel

    Perfect!!

    Thank You.

    Marking the thread as solved!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1