5

I have an excel workbook with two shapes on Sheet1 like below enter image description here

My Requirement is when the user is navigating towards right side of sheet i.e. Towards headers24, header25 and so on ,I want the two shapes on the sheet to move towards the right side with the user.

Can someone Please suggests any ideas for this.

Thanks

4
  • This is easy if you move right/left with the Arrow keys or the Tab key or the Enter key...............not so easy if you use the scroll bars. Jun 7, 2016 at 11:14
  • @Gary'sStudent: I plan to hide the scroll bar, So user will have to navigate using arrow key only Jun 7, 2016 at 11:16
  • I will post some sample code later (when I get on a computer) .............we can use an Event Macro to move the Shape when the ActiveCell moves. Jun 7, 2016 at 11:20
  • @Gary'sStudent : Yes Please that ll be very helpful Jun 7, 2016 at 12:52

2 Answers 2

7

Try this.. yep, its easy..

Place this code in the worksheet module where the shapes exist.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    With ActiveSheet.Shapes(1)

        .Left = ActiveWindow.VisibleRange(2, 2).Left
        .Top = ActiveWindow.VisibleRange(2, 2).Top

    End With

End Sub

The coordinate (2, 2) is where you want the shape to be fixed at as you scroll along with the keyboard.

But, it would be annoying to work without the scroll bar on a huge worksheet. so alternatively I think you can use refresh ontime, place this code in a Module

Private eTime
Sub ScreenRefresh()
    With ThisWorkbook.Worksheets("Sheet1").Shapes(1)
        .Left = ThisWorkbook.Windows(1).VisibleRange(2, 2).Left
        .Top = ThisWorkbook.Windows(1).VisibleRange(2, 2).Top
    End With
End Sub

Sub StartTimedRefresh()
    Call ScreenRefresh
    eTime = Now + TimeValue("00:00:01")
    Application.OnTime eTime, "StartTimedRefresh"
End Sub

Sub StopTimer()
    Application.OnTime eTime, "StartTimedRefresh", , False
End Sub

And the following code in Sheet1 (where the shapes are in)

Private Sub Worksheet_Activate()
    Call StartTimedRefresh
End Sub

Private Sub Worksheet_Deactivate()
    Call StopTimer
End Sub
7
  • Nice idea tying the Shape to ActiveWindow (+1) Jun 7, 2016 at 13:25
  • Very Nice example. It will be helpful if you can explain first part of your solution Jun 7, 2016 at 14:26
  • @RohitSaluja, what problem are you facing? are you able to get the first solution running? if you are not sure you have to place the code inside the worksheet module (e.g. Sheet1) where the shapes exist.
    – Rosetta
    Jun 7, 2016 at 14:37
  • @Rosetta: The code is running fine, but I also want to understand how is it running. So that in Future if something similar arises, I ll be able to code it on my own Jun 7, 2016 at 14:41
  • Perhaps ActiveWindow is lesser called compared to ActiveSheet. Try run debug.print ActiveWindow.VisibleRange.Address, you'll get the range address of visible cells on your "screen" only. Hence my solution use this address to fix the position of the shape.
    – Rosetta
    Jun 7, 2016 at 14:52
3

First create the shape:

Sub Creator()
    Dim shp As Shape

    Set shp = ActiveSheet.Shapes.AddShape(1, 100, 10, 60, 60)
    shp.TextFrame.Characters.Text = "I will follow"
    shp.Name = "MyButton"
End Sub

Then in the worksheet code area:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim sh As Shape, r As Range
    Set sh = ActiveSheet.Shapes("MyButton")
    Set r = ActiveCell
    sh.Top = r.Offset(-1, -2).Top
    sh.Left = r.Offset(-1, -2).Left
End Sub

If you move the active cell back and forth, the box will move with it.

Note:

This is only demo code. You still need to:

  • add protection to prevent trying to move the Shape "off-screen"
  • setting the proper offsets from ActiveCell based on the size of the Shape
4
  • please elaborate sh.Top = r.Offset(-1, -2).Top What this line of code is doing ? Jun 7, 2016 at 13:48
  • @RohitSaluja It move the shape's top to a cell two cells to left and one cell below the activecell.....so if the activecell is F12, the reference cell is D11 Jun 7, 2016 at 13:54
  • Well, I get it when you do sh.Top to set the Top Position of shape. I am also fine with r.offset(-1,-2) which says offset by one cell to the left and 2 columns to the left but why the syntax r.offset(-1,-2).Top ? Jun 7, 2016 at 14:21
  • @rohit saluja, try going to vbe and press F1 when weing on .top. For very simple and obvious questions, its better to do a bit of work oneself, no ? Jun 8, 2016 at 9:40

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.