Printing error

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Printing error

Post by ABabeNChrist »

I have a workbook with mutable sheets, all of the sheets are hidden (except 1 of course) I then have a UserForm where selections can be made to unhide desired selected sheets. When the report is ready to print I use this code.

Code: Select all

    Dim wssheet As Worksheet

    Application.ScreenUpdating = False
     For Each wssheet In ActiveWorkbook.Worksheets
      Next wssheet
        If CheckBox1.Value = True Then Sheets("Sheet1").Select Replace:=True
        If CheckBox2.Value = True Then Sheets("Sheet2").Select Replace:=False
        If CheckBox3.Value = True Then Sheets("Sheet3").Select Replace:=False

    ActiveWindow.SelectedSheets.PrintOut

     For Each wssheet In ActiveWorkbook.Worksheets
      Next wssheet
    Application.ScreenUpdating = True
The problem I have is, if a wrong selection was accidently made (this sheet may still hidden) using the checkboxes for printing, I would get and error. Is there anything I can add to this code that would skip checkbox selection if sheet is hidden.

User avatar
HansV
Administrator
Posts: 78594
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Printing error

Post by HansV »

You can remove both occurrences of

For Each wssheet In ActiveWorkbook.Worksheets
Next wssheet

Since there is nothing between the For and Next lines, they serve no purpose.

Change the line

If CheckBox1.Value = True Then Sheets("Sheet1").Select Replace:=True

to

If CheckBox1.Value And Sheets("Sheet1").Visible = xlSheetVisible Then Sheets("Sheet1").Select Replace:=True

and similar for the others.
Best wishes,
Hans

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Printing error

Post by ABabeNChrist »

Thank you Hans