Forum Discussion
How to prevent selected cells from printing, but have them visible?
- Apr 29, 2020
BobsYourUncle54 Add a new cell style (see https://jkp-ads.com/articles/styles02.asp). Name the style HideWhenPrinting. Uncheck all boxes in the new style dialog, then click the Format button. On the Font tab, change the font to white and immediately change it back to Automatic. Click OK twice.
Highlight all cells which need to be absent from the print and set their style to the new HideWhenPrinting style.
Now hit alt+F11 to go to the VBA Editor. Double-click on the ThisWorkbook module in the project explorer, past this code:
Option Explicit Private Sub Workbook_BeforePrint(Cancel As Boolean) HideCells Application.OnTime Now, "'" & ThisWorkbook.FullName & "'!ShowAgain" End Sub
Insert a new module (Insert, Module form the menu) and paste this code:
Option Explicit Sub HideCells() With ActiveWorkbook.Styles("HideWhenPrinting").Font .ThemeColor = 1 End With End Sub Public Sub ShowAgain() With ActiveWorkbook.Styles("HideWhenPrinting").Font .ColorIndex = xlAutomatic End With End Sub
That should do it. I've attached a sample workbook with everything in place.
Since you seem familiar with conditional formatting, let me suggest that you have a cell "off camera" where you can enter something along the lines of "Print" and then use conditional formatting in the subject cell so that it whites out the figure when that cell contains "Print"
My contingency planning mindset, though, says I need to ask you "How serious would it be if somebody inadvertently prints with these figures visible and shows it to the customer?"
I ask because even if the probability is low, if it would be very serious, you might want to consider a more radical preventive action. We all get forgetful at times....over time...and I for one would consider it quite likely that the day will come when somebody innocently forgets to hide that cell. At that point--again, if it's serious--you'll wish you had done something else; like creating one layout clearly marked for internal use only, and another for customers. Use the same data--don't get redundant on that--but have two different output documents.