循环通过一系列的单元格

我需要从c27和每个连续的非空单元格向右运行functionAddColumnofData函数,但我得到一个“运行时错误424对象所需”,任何帮助,不胜感激。

Set col = Range("$C$27", Range("$C$27").End(xlToRight)) For Each c In col AddColumnofData (c) Next c 

假设你已经定义了AddColumnofData

 Sub AddColumnofData(c As Range) ... End Sub 

你的电话需要

 AddColumnofData c 

(即去掉括号)

杰西的build议DIM你的variables,而不是manditory是好build议。 也适用于col 。 为了使其成为manditory,将Option Explicit添加到模块的顶部。

您可以在将它们传递给函数之前声明或设置您的范围对象。 为了certificate你正在将正确的值传递给你的函数,试试这个。

 Dim r As Range '-- if you don't declare it as a range type you get a variant type as default Dim c As Range '-- this is used to store the single cell in the For Each loop Set r = Range("A1:D1") '-- substitute your range as per your example For Each c In r '-- you could also use r.cells MsgBox c.Value '-- pass to your function instead of a call to the Message Box Next 

这将生成一系列4个消息框,其中包含活动工作表的单元格A1到D1中的值,如果范围“r”非常大,则将其传递给Debug.Print。

如果你声明c:

 Dim c as Range 

那么你有什么工作。