How do i make a shape float in the screen?

bluefeather8989

Active Member
Joined
Nov 20, 2009
Messages
327
Office Version
  1. 365
Platform
  1. Windows
How do I make Rectangle 1 float so that it stays in the upper left corner of the screen even when scrolling up/down or left/rite?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try this a bear minimum code :

In the ThisWorkbook Module:
VBA Code:
Option Explicit

Private WithEvents cmbrsEvents As CommandBars

Private Sub Workbook_Open()
    Call HookCommandBars
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    Call HookCommandBars
End Sub

Private Sub HookCommandBars()
    Set cmbrsEvents = Application.CommandBars
End Sub

Private Sub cmbrsEvents_OnUpdate()
    Static n As Double
    Static m As Double
    With ActiveWindow.VisibleRange
        If n <> .Left Or .Top <> m Then
            Sheet1.Shapes("Rectangle 1").Left = .Left
            Sheet1.Shapes("Rectangle 1").Top = .Top
        End If
        n = .Left: m = .Top
    End With
End Sub

The above workaround code works but it is not smooth. A proper solution would be to subclass the workbook scrollbars or if you are more adventarous, you may try using a more elaborate solution I posted in this thread: Cool Class for making Floating Shapes !
 
Last edited:
Upvote 0
Try this a bear minimum code :

In the ThisWorkbook Module:
VBA Code:
Option Explicit

Private WithEvents cmbrsEvents As CommandBars

Private Sub Workbook_Open()
    Call HookCommandBars
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    Call HookCommandBars
End Sub

Private Sub HookCommandBars()
    Set cmbrsEvents = Application.CommandBars
End Sub

Private Sub cmbrsEvents_OnUpdate()
    Static n As Double
    Static m As Double
    With ActiveWindow.VisibleRange
        If n <> .Left Or .Top <> m Then
            Sheet1.Shapes("Rectangle 1").Left = .Left
            Sheet1.Shapes("Rectangle 1").Top = .Top
        End If
        n = .Left: m = .Top
    End With
End Sub

The above workaround code works but it is not smooth. A proper solution would be to subclass the workbook scrollbars or if you are more adventarous, you may try using a more elaborate solution I posted in this thread: Cool Class for making Floating Shapes !
this only works if the shape is selected. i need it to work without it being selected.
 
Upvote 0
this only works if the shape is selected. i need it to work without it being selected.
If you move throughout the worksheet using arrow keys, etc., it will trigger the code and update the positioning of the shape. As you say, though, using the scrollbar does not appear to trigger the CommandBar update routine which is annoying. An alternative is the solution provided at the hyperlink at the bottom of Jaafar's reply. Subclassing is quite a precarious option and not for the faint of heart.
 
Upvote 0

Forum statistics

Threads
1,221,470
Messages
6,160,029
Members
451,611
Latest member
PattiButche

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top