VBA循环excel表列

我有一个Excel表格,其中第一个字段是一个ID。 我想循环检查第一列是否匹配。

问题是如何实现这一点,因为我不知道结束范围。

提前致谢!

或者,你可能想使用Find和FindNext

'Here you might want to deduce the range using .End(xlUp).Row with '65,535 in 2003- or about 1,000,000 as a starting point in 2007+ With Worksheets(4).Range("A:A") Set rng = .Find What:="what you're looking for" While Not rng Is Nothing 'do something Set rng = .FindNext(rng) Wend End With 

(未经testing,但不应该难以使其工作)

经过一些更多的研究,我find了答案:

 For Each c In Worksheets(4).Columns("A").Cells 

我使用“工作表(4)”,因为表格在工作表4中。

您可以遍历表中任何列的单元格,而不需要行数。 如果该表位于工作簿的工作表Sheet1中:

 Dim rngCol as Range Dim cl as Range Set rngCol = Sheet1.Range("TableName[ColumnName]") For Each cl in rngCol perform match of some type Next cl 

上面的代码将循环遍历数据值,不包括标题行和总计行。 没有必要指定表中的行数。