Lester has a macro that does its work based on what the user selected before the macro was executed. The macro needs to perform different steps based on whether the selection is a single cell, a regular range, an entire row, an entire column, or a non-contiguous range. Lester wonders if there is a way his macro can determine the type of selection that the user made.
There is a way to do this by logically testing the various properties of the selection. The best bang for the buck (so to speak) would be to create a function that could return a value based on the characteristics of the selection. Consider, for instance, the following:
Function GetSelectionType() As Long
' Returns a value that indicates the type of selection
' 0 = cells not selected
' 1 = normal contiguous range
' 2 = non-contiguous range
' 3 = single cell
' 4 = entire single row
' 5 = multiple entire rows
' 6 = entire single column
' 7 = multiple entire columns
' 8 = entire worksheet
If TypeName(Selection) <> "Range" Then
GetSelectionType = 0
Exit Function
End If
GetSelectionType = 1 ' Assume normal range
With Selection
Select Case True
Case .Areas.Count > 1
GetSelectionType = 2
Case .CountLarge = 1
GetSelectionType = 3
Case .CountLarge = .Worksheet.Cells.CountLarge
GetSelectionType = 8
Case .Columns.Count = .Worksheet.Columns.Count
GetSelectionType = 5
If .Rows.Count = 1 Then GetSelectionType = 4
Case .Rows.Count = .Worksheet.Rows.Count
GetSelectionType = 7
If .Columns.Count = 1 Then GetSelectionType = 6
End Select
End With
End Function
Notice that there are nine possible values returned, 0 through 8. A 0 value is returned only if there is something non-range related that is selected, like a chart or a graphic object. Other than that, the function checks different characteristics of the range, such as the number of areas, cells, columns, and rows. Based on what is discovered, the appropriate values are returned. Assuming that Lester calls this function from within his macro, he can then take whatever action he deems appropriate based on the value returned.
One final note: Notice that the .CountLarge property is used multiple times within the function. Those who have been around Excel a long time (well, a couple of decades or so) may remember when the .Count function was used to determine how many cells were in a selection. The internal data type used for the .Count function is a Long, which means it can only return a value up to just over 2.1 billion. However, a worksheet can contain over 17 billion cells, which means that a selection could easily be over 2.1 billion cells. Thus, Microsoft introduced the .CountLarge property in Excel 2007 to handle extremely large cell counts.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (14011) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 365 applications with VBA programming. Written in clear terms and understandable language, the book includes systematic tutorials and contains both intermediate and advanced content for experienced VB developers. Designed to be comprehensive, the book addresses not just one Office application, but the entire Office suite. Check out Mastering VBA for Microsoft Office 365 today!
Excel keeps track of the actions you take so that you can undo those actions if any are taken in error. You may want to ...
Discover MoreOne way you can use macros in a workbook is to have them automatically triggered when certain events take place. Here's ...
Discover MoreThe undo list can be a lifesaver when working in a macro. Unfortunately, the undo list is not preserved when you run a ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2026 Sharon Parq Associates, Inc.
Comments