Check Whether Excel Shapes Are Selected with VBA
Question details
Determine whether the current Excel selection includes a shape by using VBA.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A user needs a VBA macro to check the current selection without relying on TypeName or selecting a shape programmatically.
- Observed behavior
- The macro should identify an accessible selected shape and handle selections that do not expose a ShapeRange.
Open the workbook in a version of Excel that supports VBA, and make sure you can access the Visual Basic Editor. Save it as an Excel Macro-Enabled Workbook (.xlsm) if you want to retain the macro.
Test Selection.ShapeRange with VBA error handling
Use this method to check the current selection without depending on TypeName(Selection).
The macro tries to access the first shape in the selection. If the selection does not expose a ShapeRange, the object remains Nothing.
In Excel, open the Developer tab and select Visual Basic. In the editor, choose Insert > Module.
Paste this code into the module: Sub CheckSelectedShape() Dim selectedShape As Object On Error Resume Next Set selectedShape = Selection.ShapeRange(1) On Error GoTo 0 If selectedShape Is Nothing Then MsgBox "No shape is selected." Else MsgBox "A shape is selected." End If End Sub
Return to the worksheet and select a shape. Choose Developer > Macros, select CheckSelectedShape, and click Run. Repeat with a cell selected to test the other result.

Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and Microsoft Office format compatibility for easier migration. The VBA check above is specific to Excel; verify macro support and behavior before relying on it in WPS.

Frequently Asked Questions
Why use Selection.ShapeRange instead of TypeName(Selection)?
Selection.ShapeRange tests whether the current selection exposes a shape range directly. TypeName describes the selection object but is not a reliable check for every shape-selection scenario.
What happens if a cell is selected instead of a shape?
The ShapeRange access fails, but On Error Resume Next suppresses that error. The selectedShape variable remains Nothing, so the macro displays "No shape is selected."
Can the check handle multiple selected shapes?
Yes. Selection.ShapeRange(1) accesses the first shape in the selected range, so the check succeeds when a shape range with at least one shape is accessible.
How do I keep the VBA macro in my workbook?
Choose File > Save As and select Excel Macro-Enabled Workbook (.xlsm). Enable macros only when you trust the workbook and its source.




