Determining a Selection Type

Written by Allen Wyatt (last updated October 10, 2026)

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.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

An Unruly Ruler

Does the ruler in Word seem to have a mind of its own? If you have problems understanding why the ruler behaves as it ...

Discover More

Going to the Corners of a Selected Range

When you select a range of cells (particularly if it is a large range of cells), you may not be quite sure if you've ...

Discover More

Copying Large, Object-Rich Worksheets from a Corrupted Workbook

Salvaging information from a corrupted workbook can be a lot of work. This tip looks at how you can approach the problem ...

Discover More

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!

More ExcelTips (ribbon)

Clearing the Undo Stack in a Macro

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 More

Triggering an Event when a Worksheet is Deactivated

One way you can use macros in a workbook is to have them automatically triggered when certain events take place. Here's ...

Discover More

Preserving the Undo List

The undo list can be a lifesaver when working in a macro. Unfortunately, the undo list is not preserved when you run a ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is seven minus 1?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.