用单元格位置说明一个variables
我有一个包含多列和多行数据的工作表。 数据的相关部分在列a中存在具有一些文本(例如,ident)的单元时开始。
我正在尝试使用if来通过单元格,直到find具有“ident”的单元格并返回其行号(并将variables分配给此行号)
我正在使用的代码:
For Each Cell In ActiveSheet.Range("A") If ActiveSheet.Cells.Value = "Ident" Then start1 = ActiveCell.Row Exit For End If Next Row
问题是,单元格术语给我一个错误(我可能引用它错误)。 在“for each”之后需要使用什么来遍历A列中的单元格,在这种情况下?
For Each cell In ActiveSheet.Range("A:A") If cell.Value = "Ident" Then start1 = cell.Row Exit For End If Next
你也可以考虑这两个进一步的改进步骤(从逻辑和速度的angular度来看):
-
步骤1
只能通过其中包含一些常量文本值的单元格循环
For Each cell In ActiveSheet.Range("A:A").SpecialCells(xlCellTypeConstants, xlTextValues) If cell.Value = "Ident" Then start1 = cell.Row Exit For End If Next
-
第2步
使用
Find()
方法并避免循环Set cell = ActiveSheet.Range("A:A").SpecialCells(xlCellTypeConstants, xlTextValues).Find(what:="ident", lookat:=xlWhole, LookIn:=xlValues, MatchCase:=True) If Not cell Is Nothing Then start1 = cell.Row
您必须同时指定
LookIn
,LookAt
和MatchValue
参数的值,并仔细select它们
循环遍历列的另一个选项。
Option Explicit Public Sub TestMe() Dim cell As Range For Each cell In ActiveSheet.Columns(1).Cells If cell.Value = "Ident" Then Debug.Print cell.Row Exit For End If Next cell End Sub