Excel VBA -查找表列中给定值的几个行号

  • 本文关键字:几个 查找 VBA Excel excel vba
  • 更新时间 :
  • 英文 :


我真的是新的VBA,我正试图回到行号,我有"FALSE"在我的工作簿的两栏。

下面的代码在一定程度上是有效的,因为它只在第一个"FALSE"值,并且当我知道有多个"fase"时,不会在消息框中进一步报告它。值在其余列中。我怎么能让所有的行都带有&;false &;值?

Dim CurrentWB As Workbook
Set CurrentWB = ActiveWorkbook
With ActiveWorkbook.Sheets("Sheet1")
Set FindRow = .Range("J:J, K:K").Find(What:="FALSE", LookIn:=xlValues)
If Not FindRow Is Nothing Then
MsgBox ("FALSE found in row:" & FindRow.Row)
Else
MsgBox ("No FALSE found")
End If

虽然对于您/用户来说,对带有"FALSE"的行进行筛选会更容易。在,这是你要找的:

Sub testFind()
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
Dim rngF As Range
Dim CurrentWB As Workbook
Dim startFind As Long
Dim startStr As String

Set CurrentWB = ActiveWorkbook
With ActiveWorkbook.Sheets("Sheet1")
Set FindRow = .Range("J:J, K:K").Find(What:="FALSE", LookIn:=xlValues)
If Not FindRow Is Nothing Then
startFind = FindRow.Row 'to get out of the infinite loop
startStr = "FALSE found in row:"
Do Until FindRow Is Nothing
If Not dict.Exists(FindRow.Row) Then 'in case there's multiple "FALSE" in the row
dict.Add FindRow.Row, FindRow.Row 'add row to dictionary in case it's the first "FALSE"
startStr = startStr & FindRow.Row & ", " 'add row to your message
End If
Set FindRow = .FindNext(FindRow) 'next find
If FindRow.Row = startFind Then: Set FindRow = Nothing 'we're back to start so we can exit the loop
startStr = Left(startStr, Len(startStr) - 2) 'get rid of the last ", "
Loop
MsgBox startStr
End If
End With
End Sub

这是未经测试的,但我确实从我自己的一艘潜艇上抓到了它,并按照你的规格修改了它。希望这对你来说更容易理解了:)

最新更新