VBA Cancel out of a message box failure.

rplohocky

Active Member
Joined
Sep 25, 2005
Messages
292
Office Version
  1. 365
Platform
  1. Windows
Hello,
I have a few pop up questions that appear prior to running the main macro. They ask questions such as did you enter in the correct email addresses and so on. The issue is I've been testing my completed macro and I tried clicking "No" on one of them and the macro continued which destroyed hours of work. Can someone look at this code below and see why it didn't stop the macro when "No" was chosen? I marked the questions by inserting astricks on top and bottom of each question.
Code:
Sub ConvertCSVToXlsxInvoices()
'This macro loops through the workbook in the prenamed folder and changes the file extension from csv to xlsx.

    Dim answer As Integer
    Dim myValue As Variant
    Dim myfile As String
    Dim oldfname As String, newfname As String
    Dim workfile
    Dim folderName As String
 '*******************************************************************************************   
'Below is a warning box that warns you to make sure you dragged all the Reports into the POP UP Folder before you move forward.
    answer = MsgBox("Did you download and drag all the Inventory Reports needed to process this report into the POP UP folder?", vbYesNo + vbQuestion, "WARNING!")
    If answer = vbYes Then 'You clicked yes, this will make the macro continue.
    Else
    Exit Sub 'exit this macro until you drag the correct WB to the POP UP folder
    End If
'********************************************************************************************** 
   
'Below is a warning box that warns you to make sure you dragged all the Invoices into the POP UP Folder before you move forward.
    answer = MsgBox("Did you download and drag all the Invoices needed to process this report into the POP UP folder?", vbYesNo + vbQuestion, "WARNING!")
    If answer = vbYes Then 'You clicked yes, this will make the macro continue.
    Else
    Exit Sub 'exit this macro until you drag the correct WB to the POP UP folder
    End If
'************************************************************************************************

'Below is a warning box that warns you to make sure you have the correct email addresses in the address area before you move forward.
    answer = MsgBox("Do you have the correct email addresses in email address area?", vbYesNo + vbQuestion, "WARNING!")
    If answer = vbYes Then 'You clicked yes, this will make the macro continue.
    Else
    Exit Sub 'exit this macro until you save the other workbooks needed to complete this macro
    End If
'************************************************************************************************

'Below is being used for the inputbox to insert a due date for the next report time.
'This requires user to insert a next due date so they dont forget.
    myValue = InputBox("Please insert the next time this report will need to be completed by!", "WARNING!! Complete before you proceed.", Range("G1").Text)
    Range("G1").Value = myValue
    Select Case StrPtr(myValue)
        Case 0
            'OK not pressed
            Exit Sub
        Case Else
            'OK pressed
            'Carries on your routine.
    End Select
'**********************************************************************************************
    
'Below is being used for the inputbox to insert a date so this macro can create a new folder using the date.
'This requires user to insert a date so a folder can be created.
    myValue = InputBox("Please insert todays date! This will create a new folder based on the date you enter.", "WARNING!! Complete before you proceed.", Range("A168").Text)
    Range("A4").Value = myValue
    Select Case StrPtr(myValue)
        Case 0
            'OK not pressed
            Exit Sub
        Case Else
            'OK pressed
            'Carries on your routine.
    End Select
'***********************************************************************************************

'Below call a macro to create a new folder based on the date in cell B4.
    Call MakeMyFolderInvoicesCombined
    Call MakeMyFolderReportsCombined
    Call MakeMyFolderFinalWorkbook
    
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    
'Capture name of current file
    myfile = ActiveWorkbook.Name
    
'Set folder name to work through
    folderName = "\\fleet.ad\data\Data1\VMSSHARE\FS\FPSCOEASSO\Temporary Fleet Reports\JM Smuckers\Invoices Depository\"
'Below adds a backslash to the end of code above to locate the folder.
        If Right(folderName, 1) <> "\" Then
          folderName = folderName & "\"
    End If
'Loop through all CSV filres in folder
    workfile = Dir(folderName & "*.CSV")
    Do While workfile <> ""
'       Open CSV file
        Workbooks.Open Filename:=folderName & workfile
'       Capture name of old CSV file
        oldfname = ActiveWorkbook.FullName
'       Convert to XLSX
        newfname = folderName & Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4) & ".xlsx"
        ActiveWorkbook.SaveAs Filename:=newfname, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        ActiveWorkbook.Close
'       Delete old CSV file
        Kill oldfname
        Windows(myfile).Activate
        workfile = Dir()
        Loop
    
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    
'Below will create a new WB which will be used for combining all the invoices together into one sheet.
'Adding New Workbook
    Workbooks.Add
   
    'MsgBox "Please research all the rows that are displayed here in RED.", , "MUST BE DONE TO COMPLETE REPORT!"
    'MsgBox "Once completed go back to the main workbook and click on the next button.", , "MUST BE DONE TO COMPLETE REPORT!"
         
End Sub
 
As long as you don't have public variables that need to be kept, end is ok.
But you can do the same thing like
Code:
Option Explicit
Public Flg As Boolean

Sub MyStart()
   Call macro1
   If Not Flg Then Call macro2
   If Not Flg Then Call macro3
End Sub
Sub macro1()
If MsgBox("Did you download and drag all the Inventory Reports needed to process this report into the POP UP folder?", vbYesNo + vbQuestion, "WARNING!") = vbNo Then: Flg = True: Exit Sub
If MsgBox("Did you download and drag all the Invoices needed to process this report into the POP UP folder?", vbYesNo + vbQuestion, "WARNING!") = vbNo Then: Flg = True: Exit Sub
If MsgBox("Do you have the correct email addresses in email address area?", vbYesNo + vbQuestion, "WARNING!") = vbNo Then: Flg = True: Exit Sub
End Sub
Sub macro2()
MsgBox True
End Sub
Sub macro3()
MsgBox True
End Sub
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top