【问题标题】:VBA Search all sheets for double clicked cell valueVBA在所有工作表中搜索双击的单元格值
【发布时间】:2016-01-26 22:43:36
【问题描述】:

前几天我学习了如何使用 VBA 双击 sheet1 中的单元格,然后它会跳转到 Sheet 2 中具有相同值的单元格。

我现在有一个类似的报告,但这次我需要双击 Sheet1 中的一个单元格,然后在同一工作簿中的每个工作表中搜索该值。

我为第一个可行的方案提供的代码在这里: 在本工作簿中:

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

如果有人知道如何在其中创建一个工作表循环,以便它在每个工作表中查找值,我将不胜感激!

谢谢! 艾米丽 我以前代码的来源:http://www.sitestory.dk/excel_vba/hyperlinks-alternative.htm

【问题讨论】:

    标签: vba excel events


    【解决方案1】:

    给你。如果没有找到,将搜索所有工作表并返回一条消息。如果找到,将激活单元格。

    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
    

    【讨论】:

    • “伟大的斯科特”再次同步。
    • @SCOTTHOLTZMAN 非常感谢您的快速回复!我尝试运行此代码,但在第一行出现错误:编译错误:未定义用户定义的类型。有什么想法吗?
    • @Emmmily - WorksheetDim ws As Worksheet 中有一个太多的e。我编辑了我的代码。再试一次。
    • 感谢您的回复。那解决了那个错误。 @ScottHoltzman 对不起,我是 VBA 的新手,所以我无法为自己调试。在“rFind.Activate”上给出:运行时错误'1004':Range 类的激活方法失败
    • 完美,这行得通。谢谢!抱歉,直到现在我才看到你的 cmets。没有意识到有一个“显示更多 cmets 按钮”
    【解决方案2】:

    将您的第二个 Sub 更改为:

    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

    【讨论】:

    • 再次用双倍斯科特的剂量击中 SO! ...啊,是的,一剂“伟大的斯科特”!
    • @ScottHoltzman 嗨,斯科特,感谢您的出色回答。它运行代码并似乎找到了匹配项,但在“rFind.Activate”中给出了:运行时错误'1004':Range 类的激活方法失败。有任何想法吗?我对另一个斯科特的回答有同样的错误。我要查找的单元格位于数据透视表中。这会搞砸吗?
    • 耶@ScottCraner 谢谢!工作完美!希望我能绿色检查两个答案,对不起:(
    【解决方案3】:

    如果您需要在整个工作簿中查找搜索词的所有实例,而不是只需要知道至少有一次出现,您可能需要在此处查看 Chip Pearson 的 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
    

    【讨论】:

      猜你喜欢
      • 2020-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多