查找匹配值的行号

2024-01-03

我一直在尝试几种不同的方法来查找行号bingo(列出并用星号分隔)但似乎都不起作用。我究竟做错了什么?在所有情况下,我都尝试寻找 Bingo 和“Bingo”。

Sub Find_Bingo()

Dim wb As Workbook
Dim ws As Worksheet
Dim FoundCell As Range
Set wb = ActiveWorkbook
Set ws = ActiveSheet

    Const WHAT_TO_FIND As String = "Bingo"

    Set FoundCell = ws.Range("A").Find(What:=WHAT_TO_FIND)
    If Not FoundCell Is Nothing Then
        MsgBox (WHAT_TO_FIND & " found in row: " & FoundCell.Row)
    Else
        MsgBox (WHAT_TO_FIND & " not found")
    End If

'************

    With Sheet1
        Set FoundCell = Cells.Find(What:=Bingo, After:=.Cells(1, 1), _
 LookIn:=xlValues, lookat:= xlPart, SearchOrder:=xlByRows, _ 
 SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
    End With

'************

Set FoundCell = Sheets("Sheet1").Columns("E").Find(Bingo, _
ActiveSheet.Cells(2, 2), LookIn:=xlValue, lookat:=xlWhole)

'************

FoundCell = Range("A:M").Find(Bingo)

'************

FoundCell = Application.WorksheetFunction.Match(Bingo, Range("A1:A200"), 0)

'************

FoundCell = Worksheets("Sheet1").Columns(1).Find(Bingo).Row

'************

Range("A:A").Find(Bingo, Range("A1")).Row

'************

ActiveWorkbook.Worksheets("Sheet1").Columns(1).Find(Bingo).Select

'************
End Sub

对于您的第一次方法更改ws.Range("A") to ws.Range("A:A")它将搜索整个 a 列,如下所示:

Sub Find_Bingo()

        Dim wb As Workbook
        Dim ws As Worksheet
        Dim FoundCell As Range
        Set wb = ActiveWorkbook
        Set ws = ActiveSheet

            Const WHAT_TO_FIND As String = "Bingo"

            Set FoundCell = ws.Range("A:A").Find(What:=WHAT_TO_FIND)
            If Not FoundCell Is Nothing Then
                MsgBox (WHAT_TO_FIND & " found in row: " & FoundCell.Row)
            Else
                MsgBox (WHAT_TO_FIND & " not found")
            End If
End Sub

对于第二种方法,您正在使用Bingo作为变量而不是字符串文字。这是我为什么添加的一个很好的例子Option Explicit到我所有代码模块的顶部,因为当您尝试运行代码时,它会引导您到这个“变量”,它是未定义的,根本不打算成为变量。

此外,当您使用With...End With你需要一段时间.在你参考之前Cells, so Cells应该.Cells。这模仿了正常的限定行为(即 Sheet1.Cells.Find..)

Change Bingo to "Bingo"并改变Cells to .Cells

With Sheet1
        Set FoundCell = .Cells.Find(What:="Bingo", After:=.Cells(1, 1), _
        LookIn:=xlValues, lookat:=xlPart, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    End With

If Not FoundCell Is Nothing Then
        MsgBox ("""Bingo"" found in row " & FoundCell.Row)
Else
        MsgBox ("Bingo not found")
End If

Update

In my

With Sheet1
    .....
End With

The Sheet1指工作表的代码名称,而不是工作表本身的名称。例如,假设我打开一个新的空白 Excel 工作簿。默认工作表只是Sheet1。我可以在代码中引用它,代码名称为Sheet1或者我可以用索引来引用它Sheets("Sheet1")。使用代号的优点是,即使更改工作表的名称,代号也不会改变。

继续这个例子,假设我重命名了Sheet1 to Data。使用Sheet1将继续工作,因为代码名称没有改变,但现在使用Sheets("Sheet1")将返回错误,并且该语法必须更新为工作表的新名称,因此需要Sheets("Data").

在 VB 编辑器中你会看到类似这样的内容:

请注意,即使我将名称更改为Data,仍然有一个Sheet1向左转。这就是我所说的代号的意思。

The Data可以通过两种方式引用工作表:

Debug.Print Sheet1.Name
Debug.Print Sheets("Data").Name

两者都应该返回Data

有关工作表代码名称的更多讨论可以找到here http://www.ozgrid.com/VBA/excel-vba-sheet-names.htm.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

查找匹配值的行号 的相关文章

随机推荐