0
我已經嘗試了查找功能,但它只搜索單個值...我需要搜索包含同一單元格內的多個值的單元格。 即......包含「新」「汽車」「紅色」的單元格如何找到包含多個值的單元格?
編輯1,這是我現在所擁有的......非常感謝您的評論和幫助。它現在正在努力,我將不勝感激進一步優化。
Private Sub Run_Click()
Dim Val As Variant, v5 As Range, Count As Long, Temp1 As String, Temp2 As String, Temp3 As String
Dim pos1, pos2, pos3 As Integer
Dim Centinel1, Centinel2 As Boolean
Centinel1 = True
While Centinel1 = True
i = 2
Val = Cells(i, 1).Value
If Val <> "" Then
Count = 0
' Gather values from source
v1 = Cells(i, 1).Value
v2 = Cells(i, 2).Value
v3 = Left(Cells(i, 3).Value, 3)
v4 = Mid(Cells(i, 3).Value, InStrRev(Cells(i, 3).Value, "-") - 2, 2)
Centinel2 = True
Temp1 = "$B$2"
While Centinel2 = True
Set v5 = Sheets("RWI").Range("B1:B1000").Find(What:=v1, After:=Range(Temp1))
pos1 = InStr(v5, v2)
pos2 = InStr(v5, v3)
pos3 = InStr(v5, v4)
Temp2 = v5.Address
GetTail1 = Mid(Temp1, InStrRev(Temp1, "$") + 1)
GetTail2 = Mid(Temp2, InStrRev(Temp2, "$") + 1)
'Check if all matches are within "Find"
If pos1 > 1 And pos2 > 1 And pos3 > 1 Then
MsgBox v5 & " " & Sheets("RWI").Range(v5.Address).Offset(, -1)
Centinel2 = False
ElseIf Temp1 > Temp2 Then
MsgBox "Description not found."
Centinel2 = False
Else
Temp1 = v5.Address
End If
Wend
i = i + 1
Centinel1 = False
Else
Centinel1 = False
End If
Wend
末次
編輯3:這是我的代碼看起來像現在......
Private Sub Run_Click()
Dim Val As Variant, v5 As Range, Count As Long, i As Long
Dim GetTail1, GetTail2 As Long
Dim Cellsave, Temp1, Temp2, Temp3, v1, v2, v3, v4, R, Sheet, v0, v22 As String
Dim pos1, pos2, pos3 As Integer
Dim Centinel1, Centinel2, Centinel3 As Boolean
If RWbutton.Value = True Then
R = "RW-"
Sheet = "RW Overflow Sheet"
ElseIf RWIbutton.Value = True Then
R = "RWI-"
Sheet = "RWI Overflow Sheet"
End If
Centinel1 = True
i = 2
If Me.ResultsCol.Value = "" Then
MsgBox "Please input valid column letter to save results at"
Else
While Centinel1 = True
Val = Sheets(Sheet).Cells(i, 1).Value
If Val <> "" Then
Count = 0
Centinel3 = False
' Gather values from source
v0 = R
v1 = "-" & Sheets(Sheet).Cells(i, 1).Value & "-"
' Check if v2 has - or (A or B)
If Sheets(Sheet).Cells(i, 2).Value Like "*-*" And (Sheets(Sheet).Cells(i, 2).Value Like "*A*" Or Sheets(Sheet).Cells(i, 2).Value Like "*B*") Then
v2 = Left(Sheets(Sheet).Cells(i, 2).Value, Application.Find("-", Sheets(Sheet).Cells(i, 2).Value) - 1) & "-"
v22 = Right(Sheets(Sheet).Cells(i, 2).Value, 1)
Centinel3 = True
ElseIf Sheets(Sheet).Cells(i, 2).Value Like "*-*" Then
v2 = "-" & Right(Sheets(Sheet).Cells(i, 2).Value, (Len(Sheets(Sheet).Cells(i, 2).Value) - InStrRev(Sheets(Sheet).Cells(i, 2).Value, "-")))
Else
v2 = Sheets(Sheet).Cells(i, 2).Value & "-"
End If
v3 = Left(Sheets(Sheet).Cells(i, 3).Value, 3)
v4 = Right(Sheets(Sheet).Cells(i, 3).Value, (Len(Sheets(Sheet).Cells(i, 3).Value) - InStrRev(Sheets(Sheet).Cells(i, 3).Value, "/")))
Cellsave = Me.ResultsCol.Value & i
Centinel2 = True
Temp1 = "$B$1"
While Centinel2 = True
Set v5 = Sheets("fnd_gfm").Range("B1:B1000").Find(What:=v0, After:=Range(Temp1))
If (Not v5 Is Nothing) Then
pos1 = InStr(v5, v1)
pos2 = InStr(v5, v2)
pos3 = InStr(v5, v3)
pos4 = InStr(v5, v4)
Temp2 = v5.Address
GetTail1 = Mid(Temp1, InStrRev(Temp1, "$") + 1)
GetTail2 = Mid(Temp2, InStrRev(Temp2, "$") + 1)
'Check if all matches are within "Find"
If pos1 > 1 And pos2 > 1 And pos3 > 1 And pos4 > 1 Then
'Check if Part Number has A or B in it
If Centinel3 = False Then
Sheets(Sheet).Range(Cellsave).Value = Sheets("fnd_gfm").Range(v5.Address).Offset(, -1)
Centinel2 = False
ElseIf Centinel3 = True Then
Sheets(Sheet).Range(Cellsave).Value = Left(Sheets("fnd_gfm").Range(v5.Address).Offset(, -1).Value, (Len(Sheets("fnd_gfm").Range(v5.Address).Offset(, -1).Value) - 1)) & v22
Centinel2 = False
Centinel3 = False
End If
ElseIf GetTail1 > GetTail2 Then
'Check when Find does not find the value
Sheets(Sheet).Range(Cellsave).Value = "Not found."
Centinel2 = False
Else
Temp1 = v5.Address
End If
Else
Sheets(Sheet).Range(Cellsave).Value = "Not found."
Centinel2 = False
End If
Wend
i = i + 1
Else
Centinel1 = False
MsgBox "Process Finished"
End If
Wend
End If
末次
嘗試一個單獨檢查單元格的循環。如果您發現特定的問題,請發佈您的代碼,我們將很樂意幫助您解決問題並加以改進。 –
就像@ASH所說的那樣,你需要循環,但是它可能會**最好是循環使用一個**的Find,然後在這個循環內測試'Find'查看找到的單元格是否與其他術語匹配。 (很大程度上取決於您的數據結構和數量。) – YowE3K
明天我就可以做到了。非常感謝! –