VBA & Macro Problems

Use VBA to Move and Hide an Excel Shape on Cell Selection

Elise WilliamsElise Williams Dec 2, 2026 868 views

Question details

Move an Excel shape to the selected cell, hide it after a short delay, and show it again when another cell is selected.

Use VBA to Move and Hide an Excel Shape on Cell Selection
Product
Microsoft Excel
Device & OS
not provided
Scenario
A worksheet should display a shape at the selected cell temporarily whenever the selection changes.
Observed behavior
The shape should appear at each newly selected cell and disappear after the specified delay.
Before you start

Use a desktop version of Excel that supports VBA, and save the workbook as an Excel Macro-Enabled Workbook (.xlsm). Add a shape to the target worksheet and use Home > Find & Select > Selection Pane to name it Marker.

Solution 1Recommended

Use SelectionChange and OnTime to control the shape

Display the shape at each selected cell and schedule it to hide after five seconds.

The worksheet event moves and shows the shape. A standard module schedules the hide procedure and cancels any previous timer so an earlier selection does not hide the shape too soon.

1
Add the timer procedures

Press Alt+F11 to open the Visual Basic Editor, then choose Insert > Module. Paste the code below. Replace Sheet1 with the target worksheet's code name shown in the Project Explorer, if different. Public nextHide As Date Public Function HideProcedureName() As String HideProcedureName = "'" & Replace(ThisWorkbook.Name, "'", "''") & "'!HideShape" End Function Public Sub CancelHide() If nextHide <> 0 Then On Error Resume Next Application.OnTime EarliestTime:=nextHide, Procedure:=HideProcedureName(), Schedule:=False On Error GoTo 0 nextHide = 0 End If End Sub Public Sub ScheduleHide() nextHide = Now + TimeSerial(0, 0, 5) Application.OnTime EarliestTime:=nextHide, Procedure:=HideProcedureName() End Sub Public Sub HideShape() nextHide = 0 Sheet1.Shapes("Marker").Visible = msoFalse End Sub

2
Add the worksheet event

In the Project Explorer, double-click the target worksheet and paste this code into its code window: Private Sub Worksheet_SelectionChange(ByVal Target As Range) CancelHide With Me.Shapes("Marker") .Left = Target.Cells(1, 1).Left .Top = Target.Cells(1, 1).Top .Visible = msoTrue End With ScheduleHide End Sub

3
Cancel the timer on close

Double-click ThisWorkbook in the Project Explorer and add this event. If a Workbook_BeforeClose event already exists, add CancelHide to that event instead. Private Sub Workbook_BeforeClose(Cancel As Boolean) CancelHide End Sub

4
Test the shape

Save the workbook, enable macros if prompted, and select a cell on the target worksheet. The shape should appear at that cell and hide after about five seconds. Select another cell to show and reposition it again.

Use SelectionChange and OnTime to control the shape
Adjust the delay: Change the 5 in TimeSerial(0, 0, 5) to another number of seconds. OnTime may run later than scheduled if Excel is busy.
Free Microsoft Office alternative

Work with spreadsheet shapes in WPS Office

WPS Office is a free, lightweight Office alternative with a familiar interface for working with spreadsheets and shapes. Open compatible Excel files to make the transition easier; VBA and macro support can vary by version and platform.

Add and position shapes in spreadsheets using familiar toolsOpen and edit Microsoft Excel formats, including XLSXUse a free, lightweight office suite with a familiar interfaceMove between common Office file formats with less disruption
microsoft office alternative - wps office

Frequently Asked Questions

How do I change the shape's name in the code?

Use Home > Find & Select > Selection Pane to check the shape name. Replace Marker in both the worksheet event and the HideShape procedure with that exact name.

What happens if I select another cell before the shape hides?

The SelectionChange event cancels the previous timer, moves the shape to the new selection, and starts a new five-second timer.

Why does the shape not move when I select a cell?

Check that macros are enabled, the SelectionChange code is in the target worksheet's code window, and the worksheet contains a shape named Marker. Also check that Excel events have not been disabled with Application.EnableEvents.

Can I use this VBA code in Excel for the web?

No. Excel for the web does not run VBA macros. Open the macro-enabled workbook in a desktop version of Excel that supports VBA.