簡體   English   中英

循環遍歷一系列細胞

[英]looping through a range of cells

我需要在c27的每個單元格上運行AddColumnofData函數,向右運行每個連續的非空單元格,但是我得到“需要運行時錯誤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

(即刪除括號)

Jesse建議DIM你的變量,而不是mandmandory是好建議。 也適用於col 要使其成為必需,請在模塊頂部添加Option Explicit

您可以在將范圍對象傳遞給函數之前聲明或設置它們。 要證明您將正確的值傳遞給函數,請嘗試此操作。

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中的值,如果您的Range“r”非常大,則將其傳遞給Debug.Print。

如果你聲明c:

Dim c as Range

然后你應該工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM