2016-06-07 75 views
-1

我想要實現的是,當我單擊包含文本的單元格時,我希望將視圖轉換爲單元格鏈接/連接的位置。將一個單元格鏈接到同一工作表中的另一個單元格Excel

例如,讓我們說我有50列。如果我要移動到列號48,35或40的右側是非常耗時的。因此,我想要一個單元格或者一個按鈕,可以將我導航到相應的列。

我試過搜索,但也許該函數被命名爲別的東西..無論如何,我希望有人可以給我一個線索,我如何做到這一點,或信息的位置。

這裏有兩張圖片描述了我想要達到的目標。 After I press this button

The result

+0

您可以隨時點擊'Trace Precedents'按鈕並雙擊它繪製的線條 - 它將帶您進入兩個單元格之間。 –

回答

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 
+0

這很好,但我想將按鈕連接到位置,而不需要輸入哪個地方也要移動。我希望用戶單擊該按鈕並將其引導到正確的位置,而無需任何文本輸入。 –

0

假設Cell A1包含您想要移動列的數量,您可以使用:

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

繼「細胞」的想法,你可以使用工作表Worksheet_Change事件

,讓您在單元格中鍵入所需的列索引‘A1’(但它可以是任何你喜歡的細胞),並讓它‘自動地’選擇所選列

發生在工作表代碼窗格下面的代碼:

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 
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 
相關問題