选择超链接目标单元格的整行并向左滚动目标单元格

时间:2019-12-17 03:55:24

标签: excel vba

单击超链接并到达目标单元格后,我想选择(突出显示)目标单元格的整个行,如果它太靠右,则向左滚动,以便目标单元格位于屏幕的中间列并可以看到其右侧约5列。在stackoverflow中浏览类似的帖子后,我使用了FollowHyperlink()。但是,该代码没有错误,但是什么也不做。.我该如何重写代码以解决该问题?

 values = ['2015','2016','2017','2018']
 mother_data = {}
 keys = range(4)
 for i in keys:
     mother_data[i] = pd.read_csv('data/pcbf1h'+values[i]+'_df.csv', sep=",", \
                      skiprows=1, header=0)

1 个答案:

答案 0 :(得分:0)

我想这就是你想要的吗?

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

Source of the retry function

我的假设

目标范围是同一工作簿中的单个单元格。如果您仍想滚动5列,则可以使用ActiveWindow.SmallScroll ToRight:=-5。根据您的要求进行修改。

注意:此代码Worksheet_FollowHyperlink位于工作表代码区域。