gpt4 book ai didi

excel - 查找匹配值的行号

转载 作者:行者123 更新时间:2023-12-02 07:06:12 24 4
gpt4 key购买 nike

我一直在尝试几种不同的方法来查找 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") 更改为 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 之前需要使用句点 .,因此 单元格应该是.Cells。这模仿了正常的限定行为(即 Sheet1.Cells.Find..)

Bingo 更改为 "Bingo" 并将 Cells 更改为 .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

更新

在我的

With Sheet1
.....
End With

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

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

在 VB 编辑器中您会看到如下内容:

code object explorer example

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

可以通过两种方式引用数据工作表:

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

两者都应返回数据

有关工作表代码名称的更多讨论可以找到 here .

关于excel - 查找匹配值的行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32190029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com