如何遍历范围加整数?


Sub test1()
Dim Rng As Range
Dim count As Integer
Set Rng = Worksheets("Sheet1").Range("A1:A10000").Find(What:="123")
'Let's assume Rng is A2 here, but it could be anything within the Range.
count = 5
'Let's assume count is 5 here, but it can be anything from 2 to 10.
'Here I want to loop from A2 to A(2+5)
End Sub

Rngcount都会改变,因为我在此之外还有另一个循环。Rng将只有一个单元格。有谁知道如何做到这一点?任何想法都值得赞赏!

我是这样理解的

Sub test1()
Dim Rng As Range
Dim count As Integer
'Set Rng = Worksheets("Sheet1").Range("A2")
Const COL = 1
Set Rng = Worksheets("Sheet1").Cells(2, COL)
count = 5
'Here I want to loop from A2 to A(2+5)
Dim i As Long
For i = 1 To count
Debug.Print Rng.Cells(i, COL).Address
Next i
End Sub

更新根据您的编辑,您可能需要 sth。 像那样

Sub test1()
Dim Rng As Range
Dim count As Integer
Set Rng = Worksheets("Sheet1").Range("A2")
Dim col As Long
col = Rng.Column
count = 5
'Here I want to loop from A2 to A(2+5)
Dim i As Long
For i = 1 To count
Debug.Print Rng.Cells(i, col).Address
Next i
End Sub

像这样的东西?

Option Explicit
Sub test1()
'Dim Rng As Range
Dim count As Integer
Dim x As Long, i As Long
'Set Rng = Worksheets("Sheet1").Range("A2")
count = 5
'Here I want to loop from A2 to A(2+5)
x = 10
For i = 1 To x
With Worksheets("Sheet1")
.Range("A" & (2 + i * 5)) = x  'or whatever You want to do
End With
Next
End Sub

最新更新