VBA如何获取下一个单元格地址

时间:2015-07-23 13:55:32

标签: excel vba excel-vba loops while-loop

我正在编写一段代码来完成循环,我想要做的是将单元格地址J2存储在一个变量中,然后每次代码运行循环时都要进入下一个单元格向下,即J2将转到J3,然后J3将转到J4,依此类推。

这就是我现在所做的并尝试过:

Index = 0
CurrentCell = Range("J2").Address

Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)

RowsCount = Application.CountA(Range("A3:A" & Rows.Count))

While Index < RowsCount

    CurrentCell = CurrentCell + 1
    Recipients = CurrentCell + ";"
    Index = Index + 1

Wend

有人可以帮忙。

3 个答案:

答案 0 :(得分:3)

不确定当前单元格要做什么(所以我已将其删除)但是设置的行条目将设置为每一行

用下面的

替换你的循环
While Index < RowsCount

    set rng = range("J2").offset(0+Index,0)
    Recipients = Recipients & rng.value & ";"
    Index = Index + 1

Wend

答案 1 :(得分:1)

猜测你正在努力实现的目标:

Dim lIndex                As Long
Dim lLastRow              As Long

Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
' find the number of the last populated row in column A
lLastRow = Cells(Rows.Count, "A").End(xlUp).Row

For lIndex = 2 To lLastRow
    ' append the values of each cell in column J to a string, separated by semicolons
    Recipients = Recipients & ";" & Cells(lIndex, "J").Value
Next lIndex
' strip off the leading semicolon by taking everything after the first character
Recipients = Mid$(Recipients, 2)

答案 2 :(得分:0)

此行应指定下一个单元格的地址:

CurrentCell = Range(CurrentCell).Offset(1, 0).Address