View Single Post
  #13   Report Post  
Posted to microsoft.public.excel.programming
GS[_6_] GS[_6_] is offline
external usenet poster
 
Posts: 1,182
Default Prevent printing of certain sheets

Thanks Garry, but the _BeforePrint event executes in full before printing,
so if I unhide the sheets, the sheets will print.


You're not getting it!...

'hide not to print sheets
'call procedure to print
'_BeforePrint is NOT where to hide not to print sheets
'unhide not to print sheets

Your reply suggests you are doing everything in one procedure. BAD IDEA!(IMO)
but still doable if you hide/print/unhide so _BeforePrint isn't even in the
picture!


Peter suggests a single procedure to show/hide the not to print sheets. I
prefer this approach wherein you pass the instruction as follows...

Const sShtsNotToPrint$ = "Sheet1,Sheet3" '//names of sheets to not print

MySub()
'...do some stuff
'Hide sheets not to be printed
Call ShowHideSheets(False)
'Print the workbook
Call PrintWorkbookProcedure
'Unhide hidden sheets
Call ShowHideSheets

Sub ShowHideSheets(Optional bVisible As Boolean = True)
Dim v
If sShtsNotToPrint = "" Then Exit Sub '//no sheets to process
For Each v In Split(sShtsNotToPrint, ",")
ActiveWorkbook.Sheets(v).Visible = bVisible
Next 'v
End Sub

...where the option services the name of the procedure in that its primary
action is 'Show' so that your code MUST pass 'False' in order to hide (ergo
'NotShow') the sheets in the not-to-print sheetnames list.

You could also make it a generic reusable procedure as follows...

Sub ShowHide_Sheets(sShtNames$, Optional bVisible As Boolean = True)
Dim v
If sShtNames = "" Then Exit Sub '//no sheets to process
For Each v In Split(sShtNames, ",")
ActiveWorkbook.Sheets(v).Visible = bVisible
Next 'v
End Sub

...so you can use it in other projects by passing it a list of sheetnames.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion