如何使用VBA代码在Excel列中查找一个值:Cells.Find

36

我需要在一个Excel表格中找到一个名为celda的值。我正在使用以下VBA代码来查找它:

Set cell = Cells.Find(What:=celda, After:=ActiveCell, LookIn:= _
    xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _
    xlNext, MatchCase:=False, SearchFormat:=False)


If cell Is Nothing Then
    'do it something

Else
    'do it another thing
End If

问题在于我需要在 Excel 列中查找特定的数值。我使用以下代码进行查找:

    Columns("B:B").Select
    Selection.Find(What:="VA22GU1", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate

但是我不知道如何将其适应于第一个VBA代码,因为我必须使用值nothing


如果你只是想知道值是否存在于某个范围内,使用 Excel 公式会更快(如果检查数百个值,则值得)。例如,如果 celda 是一个数字,你可以使用 IF Evaluate("COUNTIF(Sheet1!A1:A1000," & celda & ")") > 0 THEN ... - lessthanideal
@SiddharthRout 注意,你上面的链接似乎已经失效了。 - eli-k
1
@eli-k: 这里是更新后的链接 :) - Siddharth Rout
4个回答

62

只需使用

Dim Cell As Range
Columns("B:B").Select
Set cell = Selection.Find(What:="celda", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

If cell Is Nothing Then
    'do it something

Else
    'do it another thing
End If

6
如果您使用以上代码出现错误,请确保在代码上方添加行 Dim cell As Range - Ben
1
不是什么大事,但变量“Cell”在声明和代码中应该大写。 - BrettFromLA

13

为了完整起见,您也可以使用上面相同的技术来处理Excel表格。

在下面的示例中,我正在查找名为“tblConfig”的Excel表格中任何单元格中的文本,该表格位于通常设置为隐藏的名为Config的工作表中。我接受查找方法的默认值。

Dim list As ListObject
Dim config As Worksheet
Dim cell as Range


Set config = Sheets("Config")
Set list = config.ListObjects("tblConfig")

'search in any cell of the data range of excel table
Set cell = list.DataBodyRange.Find(searchTerm)

If cell Is Nothing Then
    'when information is not found
Else
    'when information is found
End If

我总是认为在 VBA 脚本中最好不要更改选择,除非更改选择是所需最终结果的一部分。当然,这个特定的答案需要更改工作簿以使用定义的表格。 - robartsd
我计划在我的一个项目中使用这种方法。我正在查看一个特定的表格。在这个表格中,有一个单元格的值为“Dispatch:”。我要查找的实际值是在这个特定单元格右边的一个或两个单元格中。我该如何返回相邻单元格中的值?(例如,A1具有值“Dispatch:”,C1具有特定调度号的数字值。我想返回数字值。) - Sean Donnahoe

10

我更倾向于直接在包含要搜索的单元格范围的范围对象上使用.Find方法。对于原始发布者的代码,它可能看起来像这样:

我更喜欢直接在包含要搜索的单元格范围的Range对象上使用.Find方法。对于原始帖子的代码,可能是这样的:

Set cell = ActiveSheet.Columns("B:B").Find( _
    What:=celda, _
    After:=ActiveCell _
    LookIn:=xlFormulas, _
    LookAt:=xlWhole, _
    SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, _
    MatchCase:=False, _
    SearchFormat:=False _
)

If cell Is Nothing Then
    'do something
Else
    'do something else
End If

我更喜欢使用更多变量(并确保声明它们),让许多可选参数使用它们的默认值:

Dim rng as Range
Dim cell as Range
Dim search as String

Set rng = ActiveSheet.Columns("B:B")
search = "String to Find"
Set cell = rng.Find(What:=search, LookIn:=xlFormulas, LookAt:=xlWhole, MatchCase:=False)

If cell Is Nothing Then
    'do something
Else
    'do something else
End If

我保留了LookIn:=LookAt::=MatchCase:=这些内容,以明确正在匹配的内容。其他可选参数控制匹配返回的顺序 - 只有在顺序对我的应用程序很重要时才会指定。


1
我更喜欢这种方式,因为它不使用我正在搜索的列上的选择。这可能会改变一些“后续逻辑”,因为它依赖于所选单元格。 - HowToTellAChild

5
Dim strFirstAddress As String
Dim searchlast As Range
Dim search As Range

Set search = ActiveSheet.Range("A1:A100")
Set searchlast = search.Cells(search.Cells.Count)

Set rngFindValue = ActiveSheet.Range("A1:A100").Find(Text, searchlast, xlValues)
If Not rngFindValue Is Nothing Then
  strFirstAddress = rngFindValue.Address
  Do
    Set rngFindValue = search.FindNext(rngFindValue)
  Loop Until rngFindValue.Address = strFirstAddress

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接