每次循环增加值1

时间:2016-06-28 08:06:28

标签: vba loops range increment

我试图从A1中的值1开始,然后将值增加1并移至A2,即A1 = 1,A2 = 2等

现在我只有代码增加A1中的值。我怎样才能每次增加值并下拉一个单元格?

Sub test()
Range("A1").Value = Range("A1").Value + 1
End Sub

3 个答案:

答案 0 :(得分:1)

试试这个

Sub Demo()
    Dim rng As Range
    Set rng = Range("A1")
    If IsEmpty(rng) Then
        rng = 0
    Else
        If Not IsEmpty(rng.Offset(1, 0)) Then
            Set rng = rng.End(xlDown)
        End If
        rng.Offset(1, 0) = rng.Value + 1
    End If
End Sub

答案 1 :(得分:0)

)

答案 2 :(得分:0)

Range("a1:a100").formula = "=row()"

Range("a1").Value2 = 1
Range("a1").AutoFill Range("a1:a20"), xlFillSeries

for counter = 0 to 20
    range("a1").offset(counter).value2 = counter +1
    range("b1").offset(counter+1).Value2 = range("b1").offset(counter).Value2 + 1
next counter