简体   繁体   中英

Selecting the entire row of the hyperlinked target cell and scrolling left the target cell

After clicking a hyperlink and arriving at a target cell, I would like to select (highlight) the entire row of the target cell and scroll left if it's too far right so that the target cell is around the middle column of the screen and about 5 columns to its right can be seen. I used FollowHyperlink() after looking through similar posts in stackoverflow. However, the code has no error but does not do anything.. How can I rewrite the code to address the issue?

Sub CreateHyperlink()
...
If i > 1 Then
    .Hyperlinks.Add ...
End If
End Sub    

Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)

ActiveWindow.ScrollColumn = Target.Column
Target.EntireRow.Select

End Sub

I think this is what you want?

Option Explicit

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    Dim ws As Worksheet
    Dim rng As Range

    '~~> Get the name of the worksheet
    Set ws = ThisWorkbook.Sheets(Split(Target.SubAddress, "!")(0))

    With ws
        '~~> Get the range
        Set rng = .Range(Split(Target.SubAddress, "!")(1))

        '~~> Unhide it, in case it is hidden
        .Visible = xlSheetVisible

        '~~> Go To that cell
        Application.Goto rng, Scroll:=True

        '~~> Select the row and activate it so that it comes in the center
        rng.EntireRow.Select
        rng.Activate
    End With
End Sub

在此处输入图片说明

My assumptions

The target range is a single cell in the same workbook. If you still want to scroll 5 columns more then you can use ActiveWindow.SmallScroll ToRight:=-5 . Amend it as per your requirement.

Note : This code Worksheet_FollowHyperlink goes in the sheet code area.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM