1

我想知道是否有人可以帮助我解决这个问题。

我有一个带有几个选项卡和一个主页的工作簿。

在主页上,我有自动填充的前 20 个问题。我想要做的是在“数字”行上自动创建超链接,当我单击它们以在工作簿中搜索该数字并转到时。类似于 CTRL+F。

编号 - 问题 - 状态   
IM123
IM124
IM145
ETC..

有人能帮我吗?

谢谢

4

2 回答 2

0

在这种情况Worksheet_SelectionChange下,您可以使用类似这样的东西来搜索数据

If Not Intersect(Target, Range("A1:A4")) Is Nothing Then
    If Target.Count = 1 Then
        Cells.Find(What:=Target.Value, LookIn:=xlFormulas, LookAt:= _
                   xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                   After:=ActiveCell, SearchFormat:=False).Activate
    End If
End If

这将移动到下一个包含与您单击的单元格相同数据的单元格,只要您在指定范围内(A1:A4)并且您只选择了一个单元格(Target.Count=1)

于 2013-02-07T17:16:37.197 回答
0

以下将搜索您选择的单元格中的任何数字。除了找到第一个结果外,它还会询问您是否要继续搜索下一个结果。这样,如果您有多个问题实例,您可以“循环”通过工作表中找到的所有结果。它还将忽略在 MainWorksheet 上找到的包含前 20 个问题的搜索结果。Cells.Find 本身只会返回找到的第一个结果,即使它在同一个工作表中。

它还会告诉您是否在每张表中找到了问题编号,以及何时检查了所有工作表(以防找不到更多结果)。如果您回复不想继续搜索,它将转到在最近检查的工作表中找到的问题编号的第一个实例。

为了模拟超链接,您需要使用工作表事件,例如 Intersect(在 Sean 的回答中描述)或 FollowHyperlink。就个人而言,我更喜欢在可能的情况下使用工作表事件的替代方法,因为它们总是在满足条件时触发,即使是无意的。例如,您可以创建一个形状或宏按钮并将宏分配给它。这样,代码只会在用户单击按钮时运行。

此外,一旦您确定了问题,请让我们知道您是否真的想做更多事情。例如,您可以自动更改相关单元格,或创建已找到的该问题(或所有问题)的所有实例的列表。

祝你好运!

'Create a string variable to store the problem status
    Dim StringProblemStatus As String

'Create a string variable to store the worksheet name of the main page containing the problem status
    Dim StringMainWorksheet

'Create a range variable to store the results of the Find method
    Dim RangeFindResults As Range

'Create an integer variable to store the current worksheet number
    Dim IntegerSheetCount As Integer

'Set the variable to the problem status stored in the current cell
    StringProblemStatus = Selection.Value

'Set the variable to the worksheet name containing the problem status
    StringMainWorksheet = ActiveSheet.Name

'Create a For/Next loop so that the following code will evaluate all worksheets in the workbook
    For IntegerSheetCount = 1 To ActiveWorkbook.Sheets.Count
    Sheets(IntegerSheetCount).Activate

'Search for the problem number within the worksheet

        'Check that the sheet being searched is not the MainWorksheet
        If ActiveSheet.Name <> StringMainWorksheet Then

            'Searches for exact matches
            Set RangeFindResults = _
            Cells.Find( _
            What:=StringProblemStatus, _
            After:=ActiveCell, _
            LookIn:=xlFormulas, _
            LookAt:=xlWhole, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, _
            MatchCase:=False, _
            SearchFormat:=False _
            )

            'Returns a message to the user if the status is not found
            If RangeFindResults Is Nothing Then
                MsgBox "Problem Status " & StringProblemStatus & " was not found in worksheet " & Sheets(IntegerSheetCount).Name
            Else

                'Returns a message to the user if the status is found
                If MsgBox("Problem Status " & RangeFindResults & " found in worksheet " & _
                Sheets(IntegerSheetCount).Name & " in cell " & " " & RangeFindResults.Address & _
                ". Continue searching?", vbYesNo) = vbNo Then

                    RangeFindResults.Activate 
                    Exit Sub

                End If

            End If

        End If

    'Move to the next worksheet
    Next IntegerSheetCount

    'Notify the user that all worksheets have been searched
    MsgBox "All worksheets searched"

End Sub
于 2013-02-07T18:19:24.127 回答