Posted by
Andrian in
VBA Excel on
03 14th, 2009 |
no responses
As you know, Excel allows multiple selections by pressing Ctrl while choosing objects or ranges. This can cause problems with some macros. For example, you can’t copy a multiple selection that consists of nonadjacent cells. (Try it if you don’t believe me.)
The following macro demonstrates how to determine whether the user made a multiple selection so that your macro can take appropriate action:
Sub MultipleSelection()
If Selection.Areas.Count > 1 Then
MsgBox “Multiple selections not allowed.”
Exit Sub
End If
‘ … [Other statements go here]
End Sub
This example uses the Areas method, which returns a collection of all objects in the selection. The Count property returns the number of objects in the collection.
Related Post