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
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
suggestions

Code:
    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 Exit Sub

Code:
    If MsgBox("Do you have the correct email addresses in email address area?", vbYesNo + vbQuestion, "WARNING!") <> vbYes Then Exit Sub

force next report date to be today or later
Code:
NextReport:
    myValue = InputBox("Please insert the next time this report will need to be completed by!", "WARNING!! Complete before you proceed.",Date)
    If myValue < Date Then GoTo NextReport
    Range("G1").Value = myValue

Why must user enter today's date?, how about avoiding input box with ...
Code:
Range("A4").Value = Date
(may require formatting differently)
 
Upvote 0
How about
Code:
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 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 Exit Sub
If MsgBox("Do you have the correct email addresses in email address area?", vbYesNo + vbQuestion, "WARNING!") = vbNo Then Exit Sub
 
Upvote 0
suggestions

Code:
    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 Exit Sub

Code:
    If MsgBox("Do you have the correct email addresses in email address area?", vbYesNo + vbQuestion, "WARNING!") <> vbYes Then Exit Sub

force next report date to be today or later
Code:
NextReport:
    myValue = InputBox("Please insert the next time this report will need to be completed by!", "WARNING!! Complete before you proceed.",Date)
    If myValue < Date Then GoTo NextReport
    Range("G1").Value = myValue

Thanks Yongle for helping! I used the code you provided above, it seems to stop the macro. Still testing.

Why must user enter today's date?, how about avoiding input box with ...
Code:
Range("A4").Value = Date
(may require formatting differently)

For this part, I need the user to input a date of there choosing to name a folder that will be created based on the date they enter. It isn't always today's date. The input allows them to create a date of August 1st which may occur over the weekend or they just want a different date all together.

Thanks for the help!
 
Upvote 0
How about
Code:
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 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 Exit Sub
If MsgBox("Do you have the correct email addresses in email address area?", vbYesNo + vbQuestion, "WARNING!") = vbNo Then Exit Sub

Thank for the help Fluff! This looks much cleaner and I will use this in my future macros. I am still confused as to why my macro kept going when I chose "No" though. My code looks like it should have stopped, mine is more cluttered but it should have stopped.

Thanks for replying!
 
Upvote 0
Ok, I've been testing this in my macro. What I see happening is when you click "No" to exit out of the process, it exits out of "that" macro but skips to the next macro in the line of macros. I have a warning box that opens when you click "Yes" to start the macro process. Then it calls for a macro named "StartMacros", which is a module that calls many macros. I created separate modules to keep everything organized because of the size of this process. I broke out specific steps in separate modules.

How can I make this exit out of the entire process when a person clicks "No" on any one of those questions?
 
Upvote 0
instead of
Code:
Exit Sub

use
Code:
End
 
Upvote 0
A word of warning when using End.
If you have any variables declared outside of the code (ie, Global, Public, Private) then you will loose the values that those variables.
 
Upvote 0
A word of warning when using End.
If you have any variables declared outside of the code (ie, Global, Public, Private) then you will loose the values that those variables.

OK but if you select "No" and want to end the macro routine does it even matter? Would you have another way to stop the process other than End?
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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