Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 90280

Exit For Each Loop on Cancel of Save Dialog Box

$
0
0

I am new to VBA and as part of my own development I'm currently in the process of re-writing some automation I wrote to completion some 6+ months ago. I have the below code which identifies the sheet name based on which sheet a button is pushed (there are two different sheet names similar to CustName_CALC and two buttons). It then goes through the remaining sheets in the workbook to find "similar" sheets.

Please note that this is a WIP so there are some variables which aren't being used ie Set wb = ThisWorkbook

The below sub PDF_Bill() is applied to the before mentioned buttons:

Public vPDFilename As Variant
Public wb As Workbook
Public ws As Worksheet
Public wsBill As Worksheet
Public wsCalc As Worksheet
Public wsStatement As Worksheet
Public sLocation As String, sCustName As String, myTitle As String, myMsg As String, InitialFileName As String, Response As String, C_Response As String

Sub PDF_Bill()

sCustName = Left(ActiveSheet.Name, InStr(ActiveSheet.Name, "_") - 1)

Set wb = ThisWorkbook
Set wsCalc = wb.Sheets(sCustName & "_CALC")

myTitle = "Save Invoice"
myMsg = "Are you sure you would like to save the "& wsCalc.Cells(1, 2).Value2 & " invoice?"
Response = MsgBox(myMsg, vbQuestion + vbOKCancel, myTitle)
Select Case Response
    Case Is = vbOK

For Each wsBill In ThisWorkbook.Worksheets
        If wsBill.Name Like sCustName & "_BILL"& "*" Then
                    Call Module1_PDF.PDF_Procedure
        End If       
    Next wsBill

    Case Is = vbCancel
        'user cancels the first popup message
        myTitle = "Invoice Cancelled!"
        myMsg = "You've cancelled the request to save the invoice!"
        C_Response = MsgBox(myMsg, vbOKOnly, myTitle)

End Select
End Sub

The above calls this:

Sub PDF_Procedure()

sCustName = Left(ActiveSheet.Name, InStr(ActiveSheet.Name, "_") - 1)
sLocation = "S:DRIVELOCATION"& wsCalc.Cells(1, 2).Value2 & "\Invoices\"

vPDFilename = Application.GetSaveAsFilename( _
InitialFileName:=sLocation _
                    & wsCalc.Cells(1, 2).Value2 _
                    & "" _
                    & MonthName(Month(Date)) _
                    & " Invoice", _
FileFilter:="PDF, *.pdf", _
Title:="Save as PDF")

If vPDFilename <> False Then
    With wsBill
        '.Visible = xlSheetVisible '[will be hidden at later state]
        '.Activate
        'predefined area for 5 page invoice - this may need to change in future.
        .PageSetup.PrintArea = "A1:S300"
    End With

    'creates the PDF
    wsBill.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=vPDFilename, _
    OpenAfterPublish:=False

    'wsBill.Visible = xlSheetHidden 'bill sheet HIDE [WILL BE USED LATER NOT SET UP]
    Else
      'if user cancels save dialog box
      myTitle = "Invoice Cancelled!"
      myMsg = "You've cancelled the request to save the invoice!"
      C_Response = MsgBox(myMsg, vbOKOnly, myTitle)
      wsCalc.Activate
      GoTo CancelProcess    
End If
CancelProcess:
Exit Sub
End Sub

The above code (with the exception of the GoTo CancelProcess since that is what I'm struggling with) works perfectly for one sheet called CustName_Bill but I also need it to work for the below scenario.

I have reviewed the following pages and more without success:

I know I need an exit statement somewhere, but I'm struggling to understand where it goes. A lot of the examples I've seen aren't complex enough to lead me to a conclusion how to resolve my issue.

My issue currently is that I have two sheets which are named similar (on purpose):

  • CustName_Bill_Type
  • CustName_Bill_Type1

the loop continues onto the next sheet ie CustName_Bill_Type1 when the user cancels the Application.GetSaveAsFilename but I need the for each loop to exit when the user cancels the firstApplication.GetSaveAsFilename dialog box.

So my question is, where do I need to put my exit to get the code to stop as soon as the user clicks cancel on the first save dialog box.


Viewing all articles
Browse latest Browse all 90280

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>