Defining action on "cancel" when using DialogSheets

borting

New Member
Joined
Jun 6, 2011
Messages
9
Hi,

I was playing around with the macro i found on this post, with the code also pasted below. It creates a temporary DialogSheet to select sheets for printing. I was working on adding additional options (like formatting, exporting, etc) for the selected sheets.

The problem I see is that when the user hits the cancel button or esc key in the dialog, I think the macro continues running through the end, probably so that it will still delete the temporary DialogSheet. This is a moot issue with the printing, because it looks like it just selects no sheets, and therefore prints no sheets.

Am I right? If so, whatever other code is added after the dialog results may still continue running, causing problems. So is there a way to define the action on cancel? I think this should be a "GoTo" for a subsequent section of code so you could continue with deleting the temporary DialogSheet but skip over any other parts.


Thanks!
Andrew

PHP:
Private Sub CommandButton1_Click()
'   Display "Printer Setup" dialog box
    Application.Dialogs(xlDialogPrinterSetup).Show
'   Option Explicit
'   Sub SelectSheets()
    Dim i As Integer
    Dim TopPos As Integer
    Dim SheetCount As Integer
    Dim PrintDlg As DialogSheet
    Dim CurrentSheet As Worksheet
    Dim cb As CheckBox
    Dim Numcop As Long
    Application.ScreenUpdating = False
'   Check for protected workbook
    If ActiveWorkbook.ProtectStructure Then
        MsgBox "Workbook is protected.", vbCritical
        Exit Sub
    End If
'   Add a temporary dialog sheet
    Set CurrentSheet = ActiveSheet
    x = CurrentSheet.Name
    Set PrintDlg = ActiveWorkbook.DialogSheets.Add
    SheetCount = 0
'   Add the checkboxes
    TopPos = 40
    For i = 1 To ActiveWorkbook.Worksheets.Count
        Set CurrentSheet = ActiveWorkbook.Worksheets(i)
'       Skip empty sheets and hidden sheets
        If Application.CountA(CurrentSheet.Cells) <> 0 And _
            CurrentSheet.Visible Then
            SheetCount = SheetCount + 1
            PrintDlg.CheckBoxes.Add 78, TopPos, 150, 16.5
                PrintDlg.CheckBoxes(SheetCount).Text = _
                    CurrentSheet.Name
            TopPos = TopPos + 13
        End If
    Next i
'   Move the OK and Cancel buttons
    PrintDlg.Buttons.Left = 240
'   Set dialog height, width, and caption
    With PrintDlg.DialogFrame
        .Height = Application.Max _
            (68, PrintDlg.DialogFrame.Top + TopPos - 34)
        .Width = 230
        .Caption = "Select sheets to print"
    End With
'   Change tab order of OK and Cancel buttons
'   so the 1st option button will have the focus
    PrintDlg.Buttons("Button 2").BringToFront
    PrintDlg.Buttons("Button 3").BringToFront
'   Get the number of print copies for each report
    Numcop = Application.InputBox("Enter number of copies to print:", _
    "How Many Copies?", 1, Type:=1)
    If Numcop = 0 Then
    ElseIf Len(Numcop) > 0 Then
        End If
'   Display the dialog box
    CurrentSheet.Activate
    Dim cnt As Integer
    Application.ScreenUpdating = True
    If SheetCount <> 0 Then
        If PrintDlg.Show Then
            For Each cb In PrintDlg.CheckBoxes
                If cb.Value = xlOn Then
                    If cnt = 0 Then
                    Worksheets(cb.Caption).Select ' Replace:=False 'Activate
                Else
                    Worksheets(cb.Caption).Select Replace:=False 'Activate
                End If
                    cnt = cnt + 1
                End If
                Next cb
                    ActiveWindow.SelectedSheets.PrintOut copies:=Numcop
                    'ActiveSheet.PrintPreview 'for debugging
        End If
    Else
        MsgBox "All worksheets are empty."
    End If
'   Delete temporary dialog sheet (without a warning)
    Application.DisplayAlerts = False
    PrintDlg.Delete
'   Reactivate original sheet
    Sheets(x).Select
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
You can use GoTo x, where x is replaced by a specific line number where you want the code to pick back up. This kb link explains it in greater detail.
 
Upvote 0
Thanks...

With your reply, I guess my specific question now is how (and where) to incorporate the GoTo... I understand On Error, but does hitting cancel constitute an error? Is there another trigger specific to hitting cancel or escape?


-A
 
Upvote 0
I believe this is where they hit cancel, correct?
Code:
Numcop = Application.InputBox("Enter number of copies to print:", _
    How Many Copies?",1,Type:=1)
If Numcop = 0 Then
ElseIf Len(Numcop) > 0 Then
End If<code style="white-space:nowrap"><code>
</code></code>

Is there omitted code that you have after "Then". How I handled a similar situation was that I set the initial value to 0 and then if the value was still zero after input was requested, "exit sub" was executed...I would suggest maybe something like this?

Code:
Numcop = Application.InputBox("Enter number of copies to print:", _
    How Many Copies?",0,Type:=1)
If Numcop = 0 Then Goto ____
ElseIf Len(Numcop) > 0 Then
End If<code style="white-space:nowrap"><code>
</code></code><code style="white-space:nowrap"><code>
</code></code>
 
Last edited:
Upvote 0
Ah.... I understand your concept and how that works in the "Numcop" section, but that is a separate dialog box.


The one i was questioning is called PrintDlg:

<code style="white-space: nowrap;"><code> </code></code>
PHP:
   Dim cnt As Integer
    Application.ScreenUpdating = True
    If SheetCount <> 0 Then
        If PrintDlg.Show Then
            For Each cb In PrintDlg.CheckBoxes
                If cb.Value = xlOn Then
                    If cnt = 0 Then
                    Worksheets(cb.Caption).Select ' Replace:=False 'Activate
                Else
                    Worksheets(cb.Caption).Select Replace:=False 'Activate
                End If
                    cnt = cnt + 1
                End If
                Next cb
                    ActiveWindow.SelectedSheets.PrintOut copies:=Numcop
                    'ActiveSheet.PrintPreview 'for debugging
        End If
    Else
        MsgBox "All worksheets are empty."
    End If

The dialog is built with OK and cancel buttons, but unlike a MsgBox, I can't figure out how to tell it what to do upon cancel.

Maybe if we used your other idea and counted how many checkboxes were checked (xlOn), if that result is zero we could use GoTo?
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,629
Members
452,933
Latest member
patv

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