VBA & Macro Problems

Check Whether Excel Shapes Are Selected with VBA

Nimra MalikNimra Malik Nov 18, 2026 869 views

Question details

Determine whether the current Excel selection includes a shape by using VBA.

How to Check Whether Excel Shapes Are Selected with 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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

In Excel, open the Developer tab and select Visual Basic. In the editor, choose Insert > Module.

2
Add the macro

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

3
Test the selection

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.

Test Selection.ShapeRange with VBA error handling
Keep error handling scoped: On Error Resume Next suppresses errors during the ShapeRange check. On Error GoTo 0 immediately restores normal error handling.
Free Microsoft Office alternative

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.

Open and edit spreadsheets in a familiar interfaceCompatible with Microsoft Excel formats such as .xlsx and .xlsWork with worksheet shapes using spreadsheet editing toolsMove everyday Office documents into a lightweight office suite
QA img-9

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.