Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (5.72 MB, 490 trang )
For example, if the AllowSorting property is True, then users can still sort unlocked cells in a
The AllowEditRanges property returns an AllowEditRanges object, discussed separately in the
18.3.2 The AllowEditRange Object
The AllowEditRange object allows a specified range of cells on a worksheet to be password
protected from editing. Once a range has been protected in this way, and the entire worksheet has
been protected, any attempt at editing cells in that range will require the password.
Here is some code that assigns a password to a range on the active worksheet. It also demonstrates
the use of the AllowEditRanges collection.
Dim ws As Worksheet
Dim i As Integer
Set ws = Application.ActiveSheet
' Remove protection
' Delete all current protection ranges
For i = 1 To ws.Protection.AllowEditRanges.Count
' Add a protection range
' Protect sheet (else protection range is not enabled)
The properties of the AllowEditRange object are:
Returns or sets the range associated with the AllowEditRange object.
Returns or sets the title (i.e. name) of the range associated with the AllowEditRange
Returns the collection of UserAccessObjects associated with the AllowEditRange object.
For more on this, see the section on the UserAccess object.
The methods of the AllowEditRange object are:
Changes the password associated with the AllowEditRange object.
Deletes the AllowEditRange object.
Unprotects the workbook.
18.3.3 The UserAccess Objects
UserAccess objects allow certain users to access a protected range without requiring the password.
For instance, if your username is steve, then the following code will allow you to access protected
Dim ws As Worksheet
Dim ua As UserAccess
Set ws = Application.ActiveSheet
' NOTE: Sheet must be unprotected for this code to work!
Set ua = ws.Protection.AllowEditRanges(1).Users.Add("steve", True)
Note that the worksheet must be unprotected for this code to run without error.
The UserAccess object has but three members: the AllowEdit Boolean property, the read-only
Name property, and the Delete method.
The UserAccessList collection holds the current UserAccess objects.
18.4 Example: Printing Sheets
We can now implement the PrintSheets feature of our SRXUtils application. Recall that at the
present time, this Print utility, located in the Print.utl add-in, simply displays a message box. To
implement this feature, we want the utility to first display a dialog box, as shown in Figure 18-7.
The list box contains a list of all sheets in the active workbook. The user can select one or more
sheets and hit the Print button to print these sheets.
Figure 18-7. Print sheets dialog
The steps to create the print utility are as follows: all the action takes place in the Print.xls
workbook, so open this workbook. When the changes are finished, you will need to save Print.xls
as Print.utl as well. If Print.utl is loaded, the only way to unload it is to unload the add-in
SRXUtils.xla (if it is loaded) and close the workbook SRXUtils.xls (if it is open).
18.4.1 Create the UserForm
Create the dialog shown in Figure 18-7 in the Print.xls workbook. Name the dialog
dlgPrintSheets and set its Caption property to "Print Sheets." Then change the
PrintSheets procedure to:
Public Sub PrintSheets()
The dlgPrintSheets dialog has two command buttons and one list box:
22.214.171.124 List box
Place a list box on the form as in Figure 18-7. Using the Properties window, set the properties
shown in Table 18-4.
Table 18-4. Nondefault Properties of the List Box
When the Cancel property of the cmdCancel button is set to True, the button is "clicked" when
the user hits the Escape key. Thus, the Escape key will dismiss the print dialog.
The MultiSelect property is set to frmMultiSelectExtended so that the user can use the
Control key to select multiple (possibly nonconsecutive) entries and the shift key to select multiple
The TabIndex property determines not only the order in which the controls are visited as the user
hits the Tab key, but also determines which control has the initial focus. Since we want the initial
focus to be on the list box, we set its tab index to 0.
126.96.36.199 Print button
Place a command button on the form as in Figure 18-7. Using the Properties window, set the
properties shown in Table 18-5.
Table 18-5. Nondefault Properties of the Print Button
188.8.131.52 Cancel button
Place another command button on the form as in Figure 18-7. Using the Properties window, set
the properties shown in Table 18-6.
Table 18-6. Nondefault Properties of the Cancel Button
18.4.2 Create the Code Behind the UserForm
Now it is time to create the code behind these controls.
184.108.40.206 The Declarations section
The Declarations section of the dlgPrintSheets UserForm should contain declarations of the
module-level variables, as shown in Example 18-1.
Example 18-1. Module-Level Variable Declarations
Dim cSheets As Integer
Dim sSheetNames() As String
220.127.116.11 Cancel button code
The Cancel button code is shown in Example 18-2.
Example 18-2. The cmdCancel_Click Event Handler
Private Sub cmdCancel_Click()
18.104.22.168 Print button code
The Print button calls the main print procedure and then unloads the form; its source code is
shown in Example 18-3.
Example 18-3. The cmdPrint_Click Event Handler