Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving Shapes as user scrolls right in Excel(VBA)

Tags:

excel

vba

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

like image 600
Rohit Saluja Avatar asked Jun 07 '16 11:06

Rohit Saluja


People also ask

How do I animate an object in Excel?

Excel also has some built-in animations that you can use. To access these, go to the Insert tab and click on the Animation drop-down menu. Here you'll find a variety of animations that you can apply to objects in your workbook, such as text boxes, shapes, and charts.


1 Answers

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
like image 122
Rosetta Avatar answered Oct 07 '22 04:10

Rosetta