Microsoft Office Tutorials and References
In Depth Information
Example 19-2. The SelectSpecial Procedure
that the current selection is inappropriate for the SelectSpecial utility and not to bother displaying
the dialog. The code in Example 19-2 (which should be stored in basMain ) will do the job.
Example 19-2. The SelectSpecial Procedure
Sub SelectSpecial()
' Check for valid selection
If TypeName(Selection) <> "Range" Then
MsgBox "Selection must be a range of worksheet cells.",
End If
End Sub
Note that we use the TypeName function. When applied to an object, as in:
the function will return the name of the object.
Next, we need a couple of module-level declarations, shown in Example 19-3 , in the form's code
Example 19-3. dlgSelectSpecial Module-Level Declarations
Option Explicit
' These are used by more than one procedure
Dim rngSearch As Range
Dim rngForUndo As Range
The Initialize event of the form is the place to initialize the controls. As Example 19-4 shows, we
first want to disable some command buttons and fill the lblSearchRange label. We also can
set the module-level variables here.
Example 19-4. The Initialize Event Procedure
Private Sub UserForm_Initialize()
cmdSelect.Enabled = False
cmdUndo.Enabled = False
lblSearchRange.Caption = "Search Range: Nothing"
Set rngSearch = Selection
Set rngForUndo = rngSearch
End Sub
The Close button simply unloads the form; its source code is shown in Example 19-5 .
Example 19-5. The cmdClose_Click Event Procedure
Private Sub cmdClose_Click()
Unload Me
End Sub
Incidentally, you can test out your progress so far (and later) by running the Initialize event. Just
place the cursor in this event and hit F5.
Search JabSto ::

Custom Search