Excel - MATCH 函数 - 多个搜索结果
Excel - MATCH function - multiple search results
我正在尝试创建一个 excel 工具来搜索位于单独 sheet 中的特定关键字并显示结果(完整 sentences/paragraphs 包括这些关键字)。截至目前,我只能设法仅显示第一个发现。我想做的是将所有发现显示在彼此下方。
我使用的功能是:
=IFERROR((INDEX(Tabulka!A:A;POZVYHLEDAT(CONCATENATE("*";FINDER!C4;"*";FINDER!C5;"*";FINDER!C6;"*";FINDER!C7;"*";FINDER!C8;"*");Tabulka!A:A;0)));"N/A")
其中 Tabulka 是一个 sheet,其中 table 和 sentences/paragraphs 位于其中,而 FINDER 是一个 sheet,我在其中定义关键字并显示搜索结果。
在 Tabulka sheet 中可以有更多的句子,包括在 FINDER sheet 中定义的相同关键词。我希望该功能将它们全部显示在彼此下方,而不仅仅是像现在这样显示第一个。
是否可以使用 and/or 而不使用 VBA 以某种方式解决此问题?
谢谢
J.
这是一个使用 Regular Expression 的示例。搜索模式是根据 C 列中的单词构建的。结果显示在 A 列和 B 列中。A 列具有单元格引用。
Sub SEARCH()
Const COL_WORDS As String = "C"
Const COL_RESULTS As String = "B"
Const SHT_RESULTS As String = "FINDER"
Const SHT_SEARCH As String = "Tabulka"
Dim wb As Workbook, wsToSearch As Worksheet, wsResults As Worksheet
Dim t0 As Single
t0 = Timer
Set wb = ThisWorkbook
Set wsToSearch = wb.Sheets(SHT_SEARCH)
Set wsResults = wb.Sheets(SHT_RESULTS)
' build array of key words without blanks
Dim i As Integer, irow As Long, iLastRow As Long
Dim words() As String, cell As Range
' last row of keywords
iLastRow = wsResults.Range(COL_WORDS & Rows.count).End(xlUp).Row
ReDim words(iLastRow)
i = 0
For Each cell In wsResults.Range(COL_WORDS & "1:" & COL_WORDS & iLastRow)
If Len(cell) > 0 Then
words(i) = cell.Value
i = i + 1
End If
Next
ReDim Preserve words(i - 1)
' build regex engine
Dim regex As Object, sPattern As String
sPattern = Join(words, "|")
Debug.Print sPattern
Set regex = CreateObject("vbscript.regexp")
With regex
.Global = True
.MultiLine = True
.Pattern = sPattern
.IgnoreCase = True
End With
' scan sheet and record results under keywords
Dim count As Integer, matches As Integer
count = 0: matches = 0
irow = iLastRow + 2
For Each cell In wsToSearch.UsedRange
If Len(cell.Value) > 0 Then
count = count + 1
If regex.test(cell.Value) Then
wsResults.Cells(irow, 1) = cell.Address
cell.Copy wsResults.Range(COL_RESULTS & irow)
irow = irow + 1
matches = matches + 1
End If
End If
Next
' format results sheet
wsResults.Columns(COL_RESULTS).ColumnWidth = 100
' performance stats
Dim sMsg As String
sMsg = count & " cells scanned and " & matches & " matches found " _
& " in " & Int(Timer - t0) & " seconds."
MsgBox sMsg, vbInformation, "Result"
End Sub
我正在尝试创建一个 excel 工具来搜索位于单独 sheet 中的特定关键字并显示结果(完整 sentences/paragraphs 包括这些关键字)。截至目前,我只能设法仅显示第一个发现。我想做的是将所有发现显示在彼此下方。
我使用的功能是:
=IFERROR((INDEX(Tabulka!A:A;POZVYHLEDAT(CONCATENATE("*";FINDER!C4;"*";FINDER!C5;"*";FINDER!C6;"*";FINDER!C7;"*";FINDER!C8;"*");Tabulka!A:A;0)));"N/A")
其中 Tabulka 是一个 sheet,其中 table 和 sentences/paragraphs 位于其中,而 FINDER 是一个 sheet,我在其中定义关键字并显示搜索结果。
在 Tabulka sheet 中可以有更多的句子,包括在 FINDER sheet 中定义的相同关键词。我希望该功能将它们全部显示在彼此下方,而不仅仅是像现在这样显示第一个。
是否可以使用 and/or 而不使用 VBA 以某种方式解决此问题?
谢谢
J.
这是一个使用 Regular Expression 的示例。搜索模式是根据 C 列中的单词构建的。结果显示在 A 列和 B 列中。A 列具有单元格引用。
Sub SEARCH()
Const COL_WORDS As String = "C"
Const COL_RESULTS As String = "B"
Const SHT_RESULTS As String = "FINDER"
Const SHT_SEARCH As String = "Tabulka"
Dim wb As Workbook, wsToSearch As Worksheet, wsResults As Worksheet
Dim t0 As Single
t0 = Timer
Set wb = ThisWorkbook
Set wsToSearch = wb.Sheets(SHT_SEARCH)
Set wsResults = wb.Sheets(SHT_RESULTS)
' build array of key words without blanks
Dim i As Integer, irow As Long, iLastRow As Long
Dim words() As String, cell As Range
' last row of keywords
iLastRow = wsResults.Range(COL_WORDS & Rows.count).End(xlUp).Row
ReDim words(iLastRow)
i = 0
For Each cell In wsResults.Range(COL_WORDS & "1:" & COL_WORDS & iLastRow)
If Len(cell) > 0 Then
words(i) = cell.Value
i = i + 1
End If
Next
ReDim Preserve words(i - 1)
' build regex engine
Dim regex As Object, sPattern As String
sPattern = Join(words, "|")
Debug.Print sPattern
Set regex = CreateObject("vbscript.regexp")
With regex
.Global = True
.MultiLine = True
.Pattern = sPattern
.IgnoreCase = True
End With
' scan sheet and record results under keywords
Dim count As Integer, matches As Integer
count = 0: matches = 0
irow = iLastRow + 2
For Each cell In wsToSearch.UsedRange
If Len(cell.Value) > 0 Then
count = count + 1
If regex.test(cell.Value) Then
wsResults.Cells(irow, 1) = cell.Address
cell.Copy wsResults.Range(COL_RESULTS & irow)
irow = irow + 1
matches = matches + 1
End If
End If
Next
' format results sheet
wsResults.Columns(COL_RESULTS).ColumnWidth = 100
' performance stats
Dim sMsg As String
sMsg = count & " cells scanned and " & matches & " matches found " _
& " in " & Int(Timer - t0) & " seconds."
MsgBox sMsg, vbInformation, "Result"
End Sub