超级单元格上的Excel中心屏幕

时间:2014-04-27 02:10:10

标签: excel vba

我已经提出了以下宏来通过跟踪从一张到另一张的超链接来使屏幕在单元格上居中。不幸的是它还没有完成。关于我做错什么的任何想法?

'Center screen to cell for map hyperlinks
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
'On Error Resume Next
Application.ScreenUpdating = False
Dim onCell As Range
Dim VisRows As Integer
Dim VisCols As Integer

Set onCell = Application.Evaluate(Target.SubAddress)
onCell.Parent.Parent.Activate
onCell.Parent.Activate
With ActiveWindow.VisibleRange
    VisRows = .Rows.Count
    VisCols = .Columns.Count
End With
With Application
    .Goto Reference:=onCell.Parent.Cells( _
        .WorksheetFunction.Max(1, onCell.Row + _
        (onCell.Rows.Count / 2) - (VisRows / 2)), _
        .WorksheetFunction.Max(1, onCell.Column + _
        (onCell.Columns.Count / 2) - _
        .WorksheetFunction.RoundDown((VisCols / 2), 0))), _
     scroll:=True
End With
onCell.Select

Application.ScreenUpdating = True
End Sub

关于我的目标表,我有隐藏的行和列,我没有使用。

1 个答案:

答案 0 :(得分:1)

说我们的目标是单击超链接,然后跳转到另一个单元格,然后将新的ActiveCell放置在屏幕中间附近。试一试:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    Application.Goto reference:=ActiveCell, scroll:=True
    With ActiveWindow
        i = .VisibleRange.Rows.Count / 2
        j = .VisibleRange.Columns.Count / 2
        .SmallScroll Up:=i, ToLeft:=j
    End With
End Sub

这依赖于以下事实:在跳转之后,设置活动窗口,目标位于右下角角落。使用SmallScroll允许我们将窗口向下移动1/2屏幕,向右移动1/2。