Friday, 4 April 2014

Goto a place in a workbook by clicking on a shape

It's simple to redirect to a section in a workbook by adding a hyperlink, but the position of the cursor on the screen could be anywhere depending on where it is coming from. To get the cursor nicely aligned at the top of the screen use the following macro:


Sub GotoSection()
Dim rng As Range

    On Error Resume Next

    Set rng = Range("rng" & Mid(Application.Caller, 4, _
        Len(Application.Caller) - 3))
    rng.Worksheet.Activate
    ActiveWindow.ScrollRow = rng.Row
    ActiveSheet.Cells(rng.Row, 1).Select
End Sub


Create a shape (although it will work with buttons etc.) and assign this macro to it. Then create a named range in the cell you want to reference and prefix it with "rng". The code above assumes a three letter prefix for the name of the calling object - in my case it is "shp" because I'm using shapes as the "buttons" for the user to click on.

The macro above strips the first 3 letters and replaces them with "rng" to create the name of the range it is looking for. It then activates the sheet that this range is on, scrolls to the relevant row and selects it.

You can use any other kind of mapping from the shape (or other object) name to identify the relevant range, including simply using a select statement, but having a "translation algorithm" it makes it easy to just add another shape and another range and everything works.

One final tip: if you want to reference the same range from more than one place, you will need to copy the shape that references it. You can then change the appearance and text of the shape as required.


No comments:

Post a Comment