将一个单元格链接到同一工作表Excel中的另一个单元格

时间:2016-06-07 10:05:03

标签: excel vba excel-vba

我想要实现的是当我点击包含文本的单元格时,我希望将视图转移到单元格链接/连接的位置。

例如,假设我有50列。如果我要向右移动到第48,35或第40列,这是非常耗时的。因此我想要一个单元格或者一个按钮来导航我到相应的列。

我尝试过搜索,但也许该功能被命名为其他东西..无论如何,我希望有人可以告诉我如何实现这一点,或者信息的位置。

以下两张图片描述了我想要实现的目标。 After I press this button

The result

4 个答案:

答案 0 :(得分:0)

这不是一个真正的链接,但是如果你将这个代码影响到一个按钮,它应该满足你的需求:

Sub test_navi()

    Dim ColNb As Integer

    ColNb = InputBox("Type the number of the column you want to go to :", "Navigation", "38")
    If ColNb > 0 Then
        ActiveSheet.Cells(2, ColNb).Select
    Else
    End If

End Sub

答案 1 :(得分:0)

假设Cell A1包含要移动列的数字,您可以使用:

col = CLng(Range("A1").Value)
ActiveWindow.ScrollColumn = col

答案 2 :(得分:0)

关注"单元"想法,您可以使用工作表Worksheet_Change事件

以便在单元格中输入所需的列索引" A1" (但它可以是你喜欢的任何一个细胞)并自动拥有它#34;选择所选列

将以下代码放在工作表代码窗格中:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim val As Long
    Dim strng As String
    Dim mixed As Boolean

    If Target.Address(False, False) = "A1" Then '<~~ just change "A1" with whatever cell address you want to type column index in
        If IsEmpty(Target) Then Exit Sub

        Application.EnableEvents = False

        GetValAndNumber Target, val, strng, mixed '"interpret" the value input in cell A1

        On Error Resume Next 'prevent possible invalid inputs could error subsequent 'Range' methods calls
        If mixed Then
            Range(Target.Value).Select
        Else
            Cells(1, Target.Value).Select
        End If
        On Error GoTo 0

        Application.EnableEvents = True
    End If
End Sub


Sub GetValAndNumber(cell As Range, valLng As Long, strng As String, mixed As Boolean)
    Dim valStrng As String, char As String
    Dim i As Long

    With cell
        For i = 1 To Len(.Value2)
            char = Mid(.Value2, i, 1)
            If char Like "[0-9]" Then
                valStrng = valStrng & char
                If strng <> "" Then Exit For
            Else
                strng = strng & char
                If valStrng <> "" Then Exit For
            End If
        Next i
    End With

    mixed = strng <> "" And valStrng <> ""
    If strng <> "" Then
        mixed = valStrng <> ""
        If mixed Then valLng = CLng(valStrng)
    Else
        If valStrng <> "" Then valLng = CLng(valStrng)
    End If
End Sub

答案 3 :(得分:0)

如果您的单元格只有一个先例,那么这两个代码可能会起作用。

Public Sub FindPrecedent()

    Dim StartCell As Range
    Set StartCell = ActiveCell
    StartCell.ShowPrecedents
    StartCell.NavigateArrow TowardPrecedent:=True, ArrowNumber:=1, LinkNumber:=1
    StartCell.Parent.ClearArrows

End Sub

Public Sub FindDependent()

    Dim StartCell As Range
    Set StartCell = ActiveCell
    StartCell.ShowDependents
    StartCell.NavigateArrow TowardPrecedent:=False, ArrowNumber:=1, LinkNumber:=1
    StartCell.Parent.ClearArrows

End Sub