Use VBA to Move and Hide an Excel Shape on Cell Selection
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.

- 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.
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.
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.
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
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
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
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.

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.

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.




