Microsoft Office Tutorials and References
In Depth Information
In this lesson you write a Workbook_BeforePrint workbook-level event that instructs Excel not to
print a particular range of confidential data that resides on a particular worksheet.
To get the sample database files you can download Lesson 12 from the book’s website at
Open a new workbook and activate Sheet3. To prepare the worksheet for this
demonstration, populate range A1:E20 with some sample data by selecting the range, typing the word
Hello , and pressing Ctrl+Enter.
On your keyboard, press Alt+F11 to go to the Visual Basic Editor, and then press Ctrl+R to
ensure that the Project Explorer window is visible.
Find the name of your workbook in the Project Explorer, and expand the folder named
Microsoft Excel Objects.
The last item at the bottom of the list of Microsoft Excel Objects is the workbook object, and
it is called ThisWorkbook . You’ll want to access the Code window for the ThisWorkbook
module, and to do that, you can either double-click the ThisWorkbook object name, or
rightclick it and select View Code.
The cursor will be blinking in the Code window of your workbook module. Directly above
that, click the down arrow belonging to the Object list, and select Workbook, which will
produce the following default lines of code in your workbook module:
Private Sub Workbook_Open()
In this example you will be writing a BeforePrint procedure, so click the other down arrow
above the Code window for the Procedure field, and select BeforePrint . VBA will produce
these lines of code, which is just what you want:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Though not imperative, unless you are planning to employ the Workbook_Open event, there’s
no reason to keep the default Private Sub Workbook_Open() and End Sub statements, so
go ahead and delete them if you like.