2016-01-25 208 views
3

有一天,我學會了如何使用VBA雙擊在Sheet1中單元格,然後它會跳轉到細胞具有相同的值在表2VBA搜索所有工作表雙單擊的單元格值

我有一個類似的報告現在,除了這次我需要雙擊Sheet1中的一個單元格,然後搜索同一工作簿中的每個工作表以獲取該值。

我有,工程的第一個場景中的代碼是在這裏: 在的ThisWorkbook:

Private Sub Workbook_SheetBeforeDoubleClick _ 
(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean) 

If Len(Target.Value) = 0 Then Exit Sub 

'If the double-clicked cell isn't in column A, we exit. 
If Target.Column <> 1 Then Exit Sub 

'Calls the procedure FindName in Module1 and passes the cell content 
Module1.FindName Target.Value 

End Sub 

在模塊1:

Sub FindName(ByVal sName As String) 
'Finds and activates the first cell 
'with the same content as the double-clicked cell. sName 
'is the passed cell content. 
Dim rColumn As Range 
Dim rFind As Range 

'Activate the sheet Contact Data. 
Worksheets("All Data").Activate 

'Set the range rColumn = column B 
Set rColumn = Columns("B:B") 

'Search column B 
Set rFind = rColumn.Find(sName) 

'If found the cell is activated. 
If Not rFind Is Nothing Then 
    rFind.Activate 
Else 
    'If not found activate cell A1 
    Range("A1").Activate 
End If 

Set rColumn = Nothing 
Set rFind = Nothing 

End Sub 

如果有人知道如何也許在此創建工作表圈所以它會在每個工作表中尋找價值,我會很感激!

謝謝! Emmily 我對以前的代碼來源:http://www.sitestory.dk/excel_vba/hyperlinks-alternative.htm

回答

4

你在這裏。如果找不到任何內容,將搜索所有工作表並返回消息。如果發現它將激活單元格。

Sub FindName(ByVal sName As String) 

    'Finds and activates the first cell in any sheet (moving left-to-right) 
    'with the same content as the double-clicked cell. sName 
    'is the passed cell content. 
    Dim rFind As Range 
    Dim ws As Worksheet 

    For Each ws In ThisWorkbook.Worksheets 

     Set rFind = ws.Columns(2).Find(sName, lookat:=xlWhole) ' look for entire match, set to xlPart to search part of cell ... 2 is column B. 

     If Not rFind Is Nothing Then 
      Dim bFound As Boolean 
      bFound = True 
      ws.Activate 
      rFind.Select 
      Exit For 
     End If 

    Next 

    If Not bFound Then MsgBox sName & " not found in any sheet." 

End Sub 
+2

「大斯科茨」再次同步 –

+0

@SCOTTHOLTZMAN非常感謝您的快速響應!我試圖運行此代碼,我在第一個錯誤行:編譯錯誤:用戶定義的類型沒有定義任何想法? – Emmily

+0

@Emmily - '工作表在'昏暗的ws作爲工作表''有一個太多'e's。我編輯我的代碼,再試一次 –

4

你的第二次更改爲:

Sub FindName(ByVal sName As String) 
'Finds and activates the first cell 
'with the same content as the double-clicked cell. sName 
'is the passed cell content. 
Dim rColumn As Range 
Dim rFind As Range 
Dim ws As Worksheet 

'Activate the sheet Contact Data. 
For Each ws In ActiveWorkbook.Worksheets 
    'Change the "Sheet1" reference to the sheet calling so it is excluded 
    If ws.Name <> "Sheet1" Then 
     'Set the range rColumn = column B 
     Set rColumn = ws.Columns("B:B") 

     'Search column B 
     Set rFind = rColumn.Find(sName) 

     'If found the cell is activated. 
     If Not rFind Is Nothing Then 
      ws.activate 
      rFind.select 
     End If 
    End If 
Next ws 
Set rColumn = Nothing 
Set rFind = Nothing 

End Sub 

這將使用For Each循環以循環工作簿中的所有表。

有關每個循環的更多信息,請參閱HERE

+3

用劑量雙斯科特再次擊中SO! ......是的,一劑「偉大的斯科特」! –

+0

@ScottHoltzman嗨斯科特,謝謝你的偉大答案。它通過代碼運行,似乎找到了匹配,但在「rFind.Activate」我得到了:運行時錯誤'1004':Range類的激活方法失敗。有任何想法嗎?我對另一個斯科特的回答也有同樣的錯誤。我試圖尋找的單元格是在一個數據透視表中。這會搞砸了嗎? – Emmily

+0

@Emmily看編輯。 –

1

如果你需要找到在整個工作簿搜索詞的所有實例,而不是僅僅有知道至少有一個次數,你可能想看看這裏芯片皮爾森的FindAll方法:

http://www.cpearson.com/excel/findall.aspx

你可以利用他的FindAllOnWorksheets如下:

Sub FindMyResults(ByVal sName as string) 
    Dim Result as Variant 
    Dim ResultRange as Range 
    Dim N as Long 

    Result = FindAllOnWorksheets(InWorkbook:=ThisWorkbook, _ 
     InWorksheets:="Sheet1:Sheet3", _ 
     SearchAddress:="$B:$B", _ 
     FindWhat:=sName, _ 
     LookIn:=xlValues, _ 
     LookAt:=xlWhole, _ 
     SearchOrder:=xlByRows, _ 
     MatchCase:=False) 

    For N = LBound(Result) To UBound(Result) 
     If Not Result(N) Is Nothing Then 'There is at least one result 
      For Each ResultRange In Result(N).Cells 

       'Do something with your results. 

      Next ResultRange 
     End If 
    Next N 

End Sub 
相關問題