如何通过vba代码Cells.Find在excel列中查找值

Jon*_*pez 28 excel vba excel-vba

我必须在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
Run Code Online (Sandbox Code Playgroud)

问题是当我必须只在excel列中找到值时.我用下一个代码找到它:

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

但我不知道如何使它适应第一个vba代码,因为我必须使用该值nothing.

And*_*eev 41

只是用

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
Run Code Online (Sandbox Code Playgroud)

  • 如果您使用上面的代码并获得错误,请确保在代码上方添加"Dim cell As Range"行. (5认同)

Már*_*les 9

为了完整起见,您还可以使用与excel表相同的技术.

在下面的示例中,我在名为"tblConfig"的Excel表格的任何单元格中查找文本,放置在通常设置为隐藏的名为Config的工作表中.我接受了Find方法的默认值.

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
Run Code Online (Sandbox Code Playgroud)


rob*_*tsd 7

我更喜欢.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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

我保留LookIn:=, LookAt::=, 并MatchCase:=明确说明正在匹配的内容。其他可选参数控制返回的订单匹配 - 如果订单对我的应用程序很重要,我只会指定那些。


小智 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
Run Code Online (Sandbox Code Playgroud)