How do i make a shape float in the screen?

bluefeather8989

Active Member
Joined
Nov 20, 2009
Messages
330
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

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
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,223,908
Messages
6,175,304
Members
452,633
Latest member
DougMo

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