Excel 获取与搜索匹配的单元格行;“字符串”;特别是没有循环的列-列具有多个匹配项
在没有循环的特定列中获取与搜索“字符串”匹配的单元格行#-列有多个匹配项” 我想在不循环的情况下获取特定列中匹配字符串的行#,因为我有50000多条记录,我不想循环每一行来找出答案Excel 获取与搜索匹配的单元格行;“字符串”;特别是没有循环的列-列具有多个匹配项,excel,vba,Excel,Vba,在没有循环的特定列中获取与搜索“字符串”匹配的单元格行#-列有多个匹配项” 我想在不循环的情况下获取特定列中匹配字符串的行#,因为我有50000多条记录,我不想循环每一行来找出答案 Sub Mismatch() Dim sht As Worksheet Set Sht5 = ThisWorkbook.Worksheets("Result") Dim FindString As String FindString = "FAIL" Sht5.A
Sub Mismatch()
Dim sht As Worksheet
Set Sht5 = ThisWorkbook.Worksheets("Result")
Dim FindString As String
FindString = "FAIL"
Sht5.Activate
Columncount = Sht5.Range(Cells(1, 1), Cells(1, 1000)).Cells.SpecialCells(xlCellTypeConstants).Count 'CODE NEED TO BE UPDATED WITH COLUMN LENGTH
'To find the column count
lastReportRow = Sht5.Range("B" & Rows.Count).End(xlUp).row
'to find the last used row
For i = 2 To Columncount + 1
Set Valuefound = Sht5.Range(Cells(2, i), Cells(lastReportRow, i)).Find(FindString, After:=Range("B2"), LookIn:=xlValues)
If Valuefound Is Nothing Then
MsgBox "Value not found"
Else
For r = 2 To lastReportRow
ActualString = Sht5.Cells(r, i).Value
If FindString = ActualString Then
MsgBox r
Else
End If
'For x = 2 To lastReportRow
Next
End If
Next
End Sub
请参见您可以替换的代码: 这:
为此:
Set Valuefound = sht5.UsedRange.Find(FindString, After:=Range("B2"), LookIn:=xlValues, lookat:=xlWhole)
If Valuefound Is Nothing Then
MsgBox "Value not found"
Else
MsgBox Valuefound.Row
End If
Valuefound.row
将为您提供准确的行。您还可以添加Valuefound.column
以获取找到的值的列号
此外,您还可以根据此添加Range.FindNext
,以访问数据中多次出现的值。您可以使用匹配:
'...
lastReportRow = Sht5.Range("B" & Rows.Count).End(xlUp).row
For i = 2 To Columncount + 1
Set rng = Sht5.Range(Sht5.Cells(2, i), Sht5.Cells(lastReportRow, i))
Do
m = Application.Match(FindString, rng, 0)
If IsError(m) Then Exit Do '<< not found: exit search for this column
Debug.Print "Found '" & FindString & "' at " & rng.Cells(m).Address
'reset search range
Set rng = Sht5.Range(rng.Cells(m+1), Sht5.Cells(lastReportRow, i))
Loop
Next i
End Sub
”。。。
lastReportRow=Sht5.Range(“B”和Rows.Count).End(xlUp).row
对于i=2到Columncount+1
设置rng=Sht5.Range(Sht5.Cells(2,i),Sht5.Cells(lastReportRow,i))
做
m=应用程序.Match(FindString,rng,0)
如果IsError(m)然后退出“Do”这段代码不起作用吗?您遇到了什么错误?代码起作用了,我不想每行检查匹配的字符串以查找行#。有什么办法吗?您可以打印Valuefound.row
来获取行号,不需要循环。请参阅下面的代码的答案,这应该对您有用。@PrakashMahadevanSankaran你的目标是什么?你可以使用数组和字典,需要循环,但它们比任何其他方法都快…想想你的最终游戏是什么?这是为什么?如果单元格失败,你只是显示一个带有行的MsgBox?xlother
vsxlPart
只要告诉find
方法,如果整个strin单元格中的g需要匹配FindString
,或者如果单元格中的字符串只有一部分需要匹配FindString
。因此“xlWhole
处理所有列”毫无意义。•此外,OP说“列有多个匹配项”“因此,没有循环就不可能找到所有匹配项。是的。。我刚刚读到了,你是对的。即使这样,我们也不应该在列中循环使用Range.FindNext
。()Range.FindNext
也仅适用于循环。因为您将需要循环和Range.FindNext
,直到找不到任何内容。正确,但这将比在所有列中循环快得多。比在每个单元格中循环快得多…可能是一个巨大的“是:”)不确定查找
的速度与使用匹配
的速度相比有多快(应进行测试)。•使用Find
时,只需注意OP,然后必须设置LookAt:=
参数xlother
或xlPart
,否则使用哪一个完全是随机的(Excel始终使用VBA或用户界面上次使用的内容)。如果省略该参数,则没有默认值。
'...
lastReportRow = Sht5.Range("B" & Rows.Count).End(xlUp).row
For i = 2 To Columncount + 1
Set rng = Sht5.Range(Sht5.Cells(2, i), Sht5.Cells(lastReportRow, i))
Do
m = Application.Match(FindString, rng, 0)
If IsError(m) Then Exit Do '<< not found: exit search for this column
Debug.Print "Found '" & FindString & "' at " & rng.Cells(m).Address
'reset search range
Set rng = Sht5.Range(rng.Cells(m+1), Sht5.Cells(lastReportRow, i))
Loop
Next i
End Sub