通过FollowHyperlink事件根据分辨率调整缩放



我的工作表在A到Z列中有数据。我在第13到49行中有超链接,可以跳转到下面行中的特定单元格。例如,第13行中的超链接将跳转到第229行。

超链接很好,直到我在另一台分辨率不同的机器上做演示。它没有跳到第229行,而是显示了第248行。

我对这个和这个进行了修补,但还没有成功。我也尝试了这个不太相关的答案,看看我是否能骗过excel。我也尝试过以下代码:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
r = ActiveCell.Row
Range(Cells(r, 1), Cells(r, 26)).Select
'Target.Range.Select
ActiveWindow.Zoom = True

如果你想把A229放在可见工作表区域的左上角,那么先经过你想要的工作表的可见部分,然后再回到它,以此欺骗Excel。

在A13中,放置一个指向A1229而非A229的超链接。

Sub setup_Hyperlinks()
    With Worksheets("Sheet1")
        With .Range("A13")
            .Hyperlinks.Delete
            .Hyperlinks.Add Anchor:=.Cells(1), Address:="", SubAddress:="Sheet1!A1229", _
                            ScreenTip:="Jump to row 229", TextToDisplay:="Row 229"
        End With
    End With
End Sub

请注意,实际的子地址目标是A1229,而不是A229

右键单击工作表的名称选项卡,然后选择"查看代码"。打开VBE时,将以下内容之一粘贴到标题类似于Book1-Sheet1(code)的工作表代码表中。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells(1, 1).Row > 1000 Then  'this will depend on how you craft the method for your own purposes
        Application.Goto _
          Reference:=Target.Cells(1, 1).Offset(-1000, 0)
        '[optional] move one row down for personal aesthetics
        'ActiveWindow.SmallScroll Down:=-1
    End If
End Sub

或者,

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    If ActiveCell.Row > 1000 Then  'this will depend on how you craft the method for your own purposes
        Application.Goto _
          Reference:=ActiveCell.Offset(-1000, 0)
        '[optional] move one row down for personal aesthetics
        'ActiveWindow.SmallScroll Down:=-1
    End If
End Sub

使用其中一个,但不能同时使用。前者在我的系统上的屏幕"闪光"似乎稍微少了一点。

我突然想到了。查看Windows(1).VisibleRange.Rows.count

您可以看到显示了多少行,向下移动,以便链接目标位于顶部。无论分辨率如何,这都应该是准确的。

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    Dim iActive As Integer
    Dim lBottom As Long
    Dim ws As Excel.Worksheet
    Set ws = ActiveWorkbook.Sheets("Sheet1")
    'Get the number of rows showing
    iActive = Windows(1).VisibleRange.Rows.count
    'Move to center of the active window
    lBottom = ActiveCell.Row + (iActive / 2) - 1
    ws.Range("A" & lBottom).Activate
End Sub

最新更新