【发布时间】: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
【问题讨论】: