【发布时间】:2023-03-09 06:30:02
【问题描述】:
我正在尝试找到一种方法来搜索包含任意顺序的多个单词的单元格。 示例:在输入框中输入“搜索单词”。我现在想搜索包含这三个单词的单元格,尽管它们不必按那个顺序排列或根本不需要相邻。
希望你明白我的意思。我有这段代码,可以很好地找到一个单词,但我被卡住了,真的不知道如何解决这个问题。我知道使用五个 If 语句的解决方案不是很简洁,但它确实有效。
Sub Set_Hyper()
' Object variables
Dim wks As Excel.Worksheet
Dim rCell As Excel.Range
Dim fFirst As String
' {i} will act as our counter
Dim i As Long
Dim MyVal As String
' Search phrase
MyVal = ActiveSheet.Range("D9")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
i = 19
' Begin looping:
' We are checking all the Worksheets in the Workbook
For Each wks In ActiveWorkbook.Worksheets
If wks.Name <> "Start" Then
' We are checking all cells, we don't need the SpecialCells method
' the Find method is fast enough
With wks.Range("A:E")
' Using the find method is faster:
' Here we are checking column "A" that only have {myVal} explicitly
Set rCell = .Find(MyVal, , , xlPart, xlByColumns, xlNext, False)
' If something is found, then we keep going
If Not rCell Is Nothing Then
' Store the first address
fFirst = rCell.Address
' Where is the answer
Do
If rCell.Column() = 1 Then
' Link to each cell with an occurence of {MyVal}
rCell.Hyperlinks.Add Cells(i, 4), "", "'" & wks.Name & "'!" & rCell.Address, TextToDisplay:=rCell.Value
rCell.Offset(0, 1).Copy Destination:=Cells(i, 5)
rCell.Offset(0, 2).Copy Destination:=Cells(i, 6)
rCell.Offset(0, 3).Copy Destination:=Cells(i, 7)
rCell.Offset(0, 4).Copy Destination:=Cells(i, 8)
' wks.Range("B" & rCell.Row & ":R" & rCell.Row).Copy Destination:=Cells(i, 5)
Set rCell = .FindNext(rCell)
i = i + 1 'Increment our counter
End If
If rCell.Column() = 2 Then
' Link to each cell with an occurence of {MyVal}
rCell.Hyperlinks.Add Cells(i, 4), "", "'" & wks.Name & "'!" & rCell.Address, TextToDisplay:=rCell.Offset(0, -1).Value
rCell.Copy Destination:=Cells(i, 5)
rCell.Offset(0, 1).Copy Destination:=Cells(i, 6)
rCell.Offset(0, 2).Copy Destination:=Cells(i, 7)
rCell.Offset(0, 3).Copy Destination:=Cells(i, 8)
' wks.Range("B" & rCell.Row & ":R" & rCell.Row).Copy Destination:=Cells(i, 5)
Set rCell = .FindNext(rCell)
i = i + 1 'Increment our counter
End If
If rCell.Column() = 3 Then
' Link to each cell with an occurence of {MyVal}
rCell.Hyperlinks.Add Cells(i, 4), "", "'" & wks.Name & "'!" & rCell.Address, TextToDisplay:=rCell.Offset(0, -2).Value
rCell.Offset(0, -1).Copy Destination:=Cells(i, 5)
rCell.Copy Destination:=Cells(i, 6)
rCell.Offset(0, 1).Copy Destination:=Cells(i, 7)
rCell.Offset(0, 2).Copy Destination:=Cells(i, 8)
' wks.Range("B" & rCell.Row & ":R" & rCell.Row).Copy Destination:=Cells(i, 5)
Set rCell = .FindNext(rCell)
i = i + 1 'Increment our counter
End If
If rCell.Column() = 4 Then
' Link to each cell with an occurence of {MyVal}
rCell.Hyperlinks.Add Cells(i, 4), "", "'" & wks.Name & "'!" & rCell.Address, TextToDisplay:=rCell.Offset(0, -3).Value
rCell.Offset(0, -2).Copy Destination:=Cells(i, 5)
rCell.Offset(0, -1).Copy Destination:=Cells(i, 6)
rCell.Copy Destination:=Cells(i, 7)
rCell.Offset(0, 1).Copy Destination:=Cells(i, 8)
' wks.Range("B" & rCell.Row & ":R" & rCell.Row).Copy Destination:=Cells(i, 5)
Set rCell = .FindNext(rCell)
i = i + 1 'Increment our counter
End If
If rCell.Column() = 5 Then
' Link to each cell with an occurence of {MyVal}
rCell.Hyperlinks.Add Cells(i, 4), "", "'" & wks.Name & "'!" & rCell.Address, TextToDisplay:=rCell.Offset(0, -4).Value
rCell.Offset(0, -3).Copy Destination:=Cells(i, 5)
rCell.Offset(0, -2).Copy Destination:=Cells(i, 6)
rCell.Offset(0, -1).Copy Destination:=Cells(i, 7)
rCell.Copy Destination:=Cells(i, 8)
' wks.Range("B" & rCell.Row & ":R" & rCell.Row).Copy Destination:=Cells(i, 5)
Set rCell = .FindNext(rCell)
i = i + 1 'Increment our counter
End If
Loop While Not rCell Is Nothing And rCell.Address <> fFirst
End If
End With
End If
Next wks
' Explicitly clear memory
Set rCell = Nothing
' Reset application settings
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
编辑: 如果在一个单元格中找到所有搜索的单词,则应显示指向该行的超链接,但如果不是,则不应匹配且不显示任何内容。所以我只在这里寻找完整的匹配。
【问题讨论】:
-
值得给ParamArray 一个镜头作为自定义函数中的参数。与 ParamArray 一样,您可以传递任意数量的单词来查找,并根据方法的内部实现返回搜索结果。我认为您需要详细说明并解释该函数在找到 3 个单词中的 1 个、0 个单词或 3/3 等时将如何工作。
-
+1 是一个非常努力的好问题。当我有机会看看速度是否可以优化时,我会看看这个。
-
您有多少单元格需要进一步询问以查看它们是否包含多个单词 - 即总单元格的 x%?这取决于您是否应该使用
Find或AutoFilter等基本解决方案等在运行检查之前隔离感兴趣的细胞。否则在工作簿中的每个单元格上运行 UDF 成本很高