VBA如何在Excel中循环一列

时间:2011-09-22 09:52:25

标签: excel vba

我知道启动单元格,我需要通过列。当下一个单元格为空时,我需要退出循环。如何在VBA代码中执行此操作?

感谢您的回复

3 个答案:

答案 0 :(得分:3)

怎么样;

'//get a range from anchor cell (a1) to the 1st empty cell in the same column;
dim r As Range, cell as Range
set r = Range(Range("A1"), Range("A1").End(xlDown))

'//loop it
for Each cell In r
    msgbox cell.Value
next

答案 1 :(得分:1)

我调整了AlexK的答案:

dim c As Range

'//loop it
for Each c In Range(Range("A1"), Range("A1").End(xlDown))
    msgbox c.Value
next

答案 2 :(得分:0)

在VBA中,基于单元格的所有内容都可以使用范围完成,使用偏移量返回您要查找的值:

Dim Anchor as Range

Set Anchor = Range("A1")

i = 0
Do 
  ' Cell in column is Anchor.Offset(0, i)
  ' i.e., Anchor.Offset(0, 0) = A1
  '       Anchor.Offset(0, 1) = B1
  '       Anchor.Offset(0, ") = C1
  '       etc.

  ' Do whatever you like with this, here!

  i = i + 1
Loop Until Anchor.Offset(0, i) = ""