+ Reply to Thread
Results 1 to 8 of 8

Surpress a Run-Time Error in Excel with a Dialog Box

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-25-2012
    Location
    Edmonton, Alberta
    MS-Off Ver
    2010
    Posts
    146

    Surpress a Run-Time Error in Excel with a Dialog Box

    Basically when I use this code to print it works great if there is a sheet to print, if there isn't a sheet to print I get a run time error, which isn't necessarily a big deal but for esthetic reasons I would like to have a dialog box come up that says "Sorry, nothing to print" if there is nothing to print.

    Sub PrintAllTransfer()
        Dim Sh As Worksheet
        Dim Arr() As String
        Dim N As Integer
        N = 0
        Application.ScreenUpdating = False
        For Each Sh In ActiveWorkbook.Worksheets
            If Sh.Range("A607").Value <> "" Then
                N = N + 1
                ReDim Preserve Arr(1 To N)
                Arr(N) = Sh.Name
            End If
        Next
        With ActiveWorkbook
            For N = 1 To UBound(Arr)
    If ActiveWorkbook.Worksheets(Arr(N)).Visible = xlSheetHidden Then
    ActiveWorkbook.Worksheets(Arr(N)).Visible = xlSheetVisible
    ActiveWorkbook.Worksheets(Arr(N)).PrintOut
    ActiveWorkbook.Worksheets(Arr(N)).Visible = xlSheetHidden
    Else
    ActiveWorkbook.Worksheets(Arr(N)).PrintOut
    End If
    Next N
    
        End With

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Surpress a Run-Time Error in Excel with a Dialog Box

    When won't there be a sheet to print?
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    11-25-2012
    Location
    Edmonton, Alberta
    MS-Off Ver
    2010
    Posts
    146

    Re: Surpress a Run-Time Error in Excel with a Dialog Box

    Quote Originally Posted by Norie View Post
    When won't there be a sheet to print?
    There can be up to 50 different (types of) pages in the workbook to print, dont want to check through entire workbook to see what there is to print, so have a pag with buttons that can print each type of page from one tab.

  4. #4
    Forum Contributor
    Join Date
    10-08-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    386

    Re: Surpress a Run-Time Error in Excel with a Dialog Box

    Right above the print command put

    on error goto NoSheet
    then at the bottom above end sub put

    NoSheet:
    msgbox "Nothing to Print"
    exit sub

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Surpress a Run-Time Error in Excel with a Dialog Box

    So there's no criteria/logic that can be used in the code to determine whether or not there are sheets to print.

    PS The code you posted loops through all the worksheets already.

  6. #6
    Forum Contributor
    Join Date
    11-25-2012
    Location
    Edmonton, Alberta
    MS-Off Ver
    2010
    Posts
    146

    Re: Surpress a Run-Time Error in Excel with a Dialog Box

    Quote Originally Posted by Norie View Post
    So there's no criteria/logic that can be used in the code to determine whether or not there are sheets to print.

    PS The code you posted loops through all the worksheets already.
    The criteria for this particular sheet is that if there is a value in cell A607 then it will print, if there is no value in A607 then it will not print.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Surpress a Run-Time Error in Excel with a Dialog Box

    Sorry you've lost me there.

    You are using the same criteria for all sheets and putting the name of each sheet that has A607 populated in an array.

    I think that checking what N is might be an idea.

    If it's 0 then no sheets have A607 populated.

    In fact I'd change the loop to this.
        For I = 1 To N
    
            With ActiveWorkbook.Worksheets(Arr(I))
                If .Visible = xlSheetHidden Then
                    .Visible = xlSheetVisible
                    .PrintOut
                    .Visible = xlSheetHidden
                Else
                    .PrintOut
                End If
            End With
    
        Next I
    If N is 0 then the code within the loop will never execute as you can go from 1 to 0.
    Last edited by Norie; 07-08-2013 at 04:50 PM.

  8. #8
    Forum Contributor
    Join Date
    11-25-2012
    Location
    Edmonton, Alberta
    MS-Off Ver
    2010
    Posts
    146

    Re: Surpress a Run-Time Error in Excel with a Dialog Box

    Thanks Norie

+ 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