VBA如何在Excel中循环浏览列

VBA如何在Excel中循环浏览列,excel,vba,Excel,Vba,我知道起始单元格,我需要看一下列。当下一个单元格为空时,我需要退出循环。如何在VBA代码中实现 感谢您的回复怎么样 '//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

我知道起始单元格,我需要看一下列。当下一个单元格为空时,我需要退出循环。如何在VBA代码中实现

感谢您的回复

怎么样

'//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
怎么样,

'//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

在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) = ""

在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) = ""

我调整了亚历克斯的回答:

dim c As Range

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

我调整了亚历克斯的回答:

dim c As Range

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

1) 我不会使用cell作为变量名。叫它迈塞尔。2) 您需要
DIM myCell as Range
添加一个DIM,I left cell as is,它不是保留字,并且在xl对象模型中不作为类名使用(尽管Cells是)@Alex:您可能对cell是正确的。我太谨慎了:-/1)我不会使用cell作为变量名。叫它迈塞尔。2) 您需要
DIM myCell as Range
添加一个DIM,I left cell as is,它不是保留字,并且在xl对象模型中不作为类名使用(尽管Cells是)@Alex:您可能对cell是正确的。我太谨慎了:-/虽然这标记为已回答,但一旦定义了范围,您想对该列做什么?我正在使用Cognos TM1,需要从OLAP获取有关单元格的评论,而这标记为已回答。一旦定义了范围,您想对该列做什么?我正在使用Cognos TM1,并且需要从OLAP获取有关单元格的评论