我目前拥有近1500行的BD。每行都有一串不同的字符(都在同一个单元格内),在某些情况下,每行中都有相似的单词。例如:
Row 1: Tempera for children of color red 10 grams
Row 2: Tempera for adults color black 50 grams
Row 3: Tempera for children of color yellow 10 grams
Row 4: Tempera for adults color black 10 grams
我正在尝试创建一个宏,在其中输入两个或更多单词,并在每行中搜索这些单词,然后,我解释哪些是包含两个或更多单词的行。例如:
如果我输入单词:
答案 0 :(得分:1)
这可能不是你想要的,但是一种方法而不是从头开始写一个宏就是使用像this one这样的连接函数和数组公式: -
=myconcat(",",IF((1+ISBLANK(B1)-ISERROR(SEARCH(B1,$A$1:$A$4)))*(2-ISBLANK(B1)-ISBLANK(C1))*(1+ISBLANK(C1)-ISERROR(SEARCH(C1,$A$1:$A$4))),ROW($A$1:$A$4),""))
其中要搜索的字符串在A1:A4中,要搜索的字在B1和C1中。
公式必须在D1中输入 Ctrl Shift Enter 并下拉。
答案 1 :(得分:1)
您可以使用以下代码:
Private Function SearchForOneStringInArr(oneString As String, arr() As String) As Boolean
Dim i
For i = 0 To UBound(arr)
If InStr(1, LCase(oneString), LCase(arr(i))) = 0 Then
SearchForOneStringInArr = False
Exit Function
End If
Next
SearchForOneStringInArr = True
End Function
Public Function mySearch(rng As Range, toSearch As String) As String
Dim i As Integer, cell As Range
i = 1
Dim strArr() As String
Dim resultStr As String
resultStr = "Rows:"
strArr = Split(toSearch, " ")
For Each cell In rng.Cells
If SearchForOneStringInArr(cell.Value, strArr) = True Then
resultStr = resultStr & Str(i) & " , "
End If
i = i + 1
Next
mySearch = resultStr
End Function
使用以下公式:
=mySearch(A1:A4, "black tempera")
=mySearch(A1:A4, "children")
=mySearch(A1:A4, "10 tempera")