Excel VBA 查找“C”列包含已知值的最后一行编号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28132471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel VBA Find last row number where column "C" contains a known value
提问by Kirk
Seeking a method in Excel VBA to Find last row number where column "C" contains a known value.
在 Excel VBA 中寻找一种方法来查找“C”列包含已知值的最后一行编号。
回答by Gary's Student
This will find the lastoccurrence of happinessin column C
这将在C列中找到最后一次出现的幸福
Sub SeekHappiness()
Dim C As Range, where As Range, whatt As String
whatt = "happiness"
Set C = Range("C:C")
Set where = C.Find(what:=whatt, after:=C(1), searchdirection:=xlPrevious)
MsgBox where.Address(0, 0)
End Sub
To output the row number only, use:
要仅输出行号,请使用:
MsgBox Mid(where.Address(0, 0), 2)
To find the firstoccurrence:
要查找第一次出现:
Sub SeekHappiness()
Dim C As Range, where As Range, whatt As String
whatt = "happiness"
Set C = Range("C:C")
Set where = C.Find(what:=whatt, after:=C(1))
MsgBox where.Address(0, 0)
End Sub
回答by Emporer
You could loop through the column to find the last occurrence of a value.
您可以遍历该列以查找最后一次出现的值。
Sub findLastRow()
Dim searchValue As String
Dim endRow As Integer
Dim lastRowSearchValue As Integer
searchValue = "testValue" ''enter your search value
With Worksheets("sheet1") ''enter the name of your worksheet
endRow = .Cells(Rows.Count, 3).End(xlUp).Row
For i = 1 To endRow
If .Cells(i, 3) = searchValue Then
lastRowSearchValue = i
End If
Next i
End With
End Sub
Just replace the value of the variable "searchValue" with whatever is the value you're looking for (maybe change the type of the variable if its not a string) and the Sub will store the index of the last row of the occurrence of the searchValue in the variable "lastRowSearchValue" for further use.
只需将变量“searchValue”的值替换为您要查找的任何值(如果它不是字符串,则可能更改变量的类型),Sub 将存储出现的最后一行的索引变量“lastRowSearchValue”中的searchValue 以供进一步使用。