excel - 在没有循环的特定列中获取与搜索“字符串”匹配的单元格的行号 - 列有多个匹配项
问题描述
在没有循环的特定列中获取与搜索“字符串”匹配的单元格的行号 - 列有多个匹配项”
我想在不循环的情况下获取特定列中匹配字符串的行号,因为我有超过 50000 条记录,我不想循环每一行来找出
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
解决方案
您可以使用匹配:
'...
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
推荐阅读
- php - Laravel,安装程序不起作用
- laravel - Laravel:捕获路径不是以api开头的所有路由
- php - 将值添加到分块数组值并输出到表
- c# - 使用迁移 Entity Framework Core 添加数据而不指定 id
- apache-spark - spark-submit 中的 num-executors、executor-cores、executor-memory 与 spark.read 中的 option(“numPartitions”,x) 有什么区别?
- javascript - 如何在javascript中基于动态复选框创建动态文本框
- android - 任务 ':app:transformDexArchiveWithExternalLibsDexMergerForDebug' 执行失败。[react-native wix 导航][v2]
- azure - ADF V2 - 基于表列参数化数据复制管道
- android - 权限拒绝:startForeground 需要 android.permission.FOREGROUND_SERVICE
- windows - 使用 UIElement 创建 Cordova UWP 插件