搜索多个条件得到多个结果
Search multiple criteria get multiple results
我正在尝试搜索 Table 的多个条件,如果我得到这些条件的多个结果,我想显示它们。
到目前为止,我的代码可以搜索多个条件,但在找到一个结果时停止。
这是代码片段:
`
Set rngSearch = Sheets(Temp_Bereich).Range("A:M")
Set Found = rngSearch.Find(What:=Material_A, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Found Is Nothing Then
Firstfound = Found.Address
Do
If Found.EntireRow.Range("B1").Value = Material_B And _
Found.EntireRow.Range("C1").Value = Schmierzustand_AB And _
Found.EntireRow.Range("G1").Value = Rauheit_A And _
Found.EntireRow.Range("H1").Value = Rauheit_B And _
Found.EntireRow.Range("D1").Value = Schmiermittel_AB Then Exit Do 'Match found
Set Found = rngSearch.FindNext(After:=Found)
If Found.Address = Firstfound Then Set Found = Nothing
Loop Until Found Is Nothing
End If
If Not Found Is Nothing Then
Application.Goto Found.EntireRow
Haftreibwert.Value = Cells(Found.Row, 12).Value
Gleitreibwert.Value = Cells(Found.Row, 13).Value
Else
MsgBox "Es trifft leider nichts auf alle 6 Kriterien zu ", , "Kein Match gefunden"
End If
您没有解释如果找到多个结果您想要做什么,但这应该是一个开始:
Dim allA As Range, c As Range
Set rngSearch = Sheets(Temp_Bereich).Range("A:M")
'call a function to return all of the matches
Set allA = FindAll(rngSearch) 'really searching entire range, or just one column?
If allA.Count = 0 Then
MsgBox "Es trifft leider nichts auf alle 6 Kriterien zu ", , "Kein Match gefunden"
Exit Sub
End If
For Each c In allA
With c.EntireRow
If .Range("B1").Value = Material_B And _
.Range("C1").Value = Schmierzustand_AB And _
.Range("G1").Value = Rauheit_A And _
.Range("H1").Value = Rauheit_B And _
.Range("D1").Value = Schmiermittel_AB Then
Debug.Print "Matched on row# " & .Row
End If
End With
Next c
如果将“查找所有匹配项”分解为一个单独的函数,那么管理逻辑会更容易。
'Find all exact matches for `val` in a supplied range and
' return as a collection of matched cells
Public Function FindAll(rng As Range, val As String) As Collection
Dim rv As New Collection, f As Range
Dim addr As String
Set f = rng.Find(what:=val, after:=rng.Cells(rng.Cells.Count), _
LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
If Not f Is Nothing Then addr = f.Address()
Do Until f Is Nothing
rv.Add f
Set f = rng.FindNext(after:=f)
If f.Address() = addr Then Exit Do
Loop
Set FindAll = rv
End Function
我正在尝试搜索 Table 的多个条件,如果我得到这些条件的多个结果,我想显示它们。
到目前为止,我的代码可以搜索多个条件,但在找到一个结果时停止。
这是代码片段: `
Set rngSearch = Sheets(Temp_Bereich).Range("A:M")
Set Found = rngSearch.Find(What:=Material_A, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Found Is Nothing Then
Firstfound = Found.Address
Do
If Found.EntireRow.Range("B1").Value = Material_B And _
Found.EntireRow.Range("C1").Value = Schmierzustand_AB And _
Found.EntireRow.Range("G1").Value = Rauheit_A And _
Found.EntireRow.Range("H1").Value = Rauheit_B And _
Found.EntireRow.Range("D1").Value = Schmiermittel_AB Then Exit Do 'Match found
Set Found = rngSearch.FindNext(After:=Found)
If Found.Address = Firstfound Then Set Found = Nothing
Loop Until Found Is Nothing
End If
If Not Found Is Nothing Then
Application.Goto Found.EntireRow
Haftreibwert.Value = Cells(Found.Row, 12).Value
Gleitreibwert.Value = Cells(Found.Row, 13).Value
Else
MsgBox "Es trifft leider nichts auf alle 6 Kriterien zu ", , "Kein Match gefunden"
End If
您没有解释如果找到多个结果您想要做什么,但这应该是一个开始:
Dim allA As Range, c As Range
Set rngSearch = Sheets(Temp_Bereich).Range("A:M")
'call a function to return all of the matches
Set allA = FindAll(rngSearch) 'really searching entire range, or just one column?
If allA.Count = 0 Then
MsgBox "Es trifft leider nichts auf alle 6 Kriterien zu ", , "Kein Match gefunden"
Exit Sub
End If
For Each c In allA
With c.EntireRow
If .Range("B1").Value = Material_B And _
.Range("C1").Value = Schmierzustand_AB And _
.Range("G1").Value = Rauheit_A And _
.Range("H1").Value = Rauheit_B And _
.Range("D1").Value = Schmiermittel_AB Then
Debug.Print "Matched on row# " & .Row
End If
End With
Next c
如果将“查找所有匹配项”分解为一个单独的函数,那么管理逻辑会更容易。
'Find all exact matches for `val` in a supplied range and
' return as a collection of matched cells
Public Function FindAll(rng As Range, val As String) As Collection
Dim rv As New Collection, f As Range
Dim addr As String
Set f = rng.Find(what:=val, after:=rng.Cells(rng.Cells.Count), _
LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
If Not f Is Nothing Then addr = f.Address()
Do Until f Is Nothing
rv.Add f
Set f = rng.FindNext(after:=f)
If f.Address() = addr Then Exit Do
Loop
Set FindAll = rv
End Function