vba 循环通过固定次数的 Excel 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25260171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:12:56  来源:igfitidea点击:

Excel loop that loops through a fixed number of times

excelvbaexcel-vba

提问by

I am working on some formulas in excel VBA and I am trying to make it loop through a certain number of times. Is it possible to do so in VBA and if so how do I do it?

我正在 excel VBA 中处理一些公式,我试图让它循环一定次数。是否可以在 VBA 中这样做,如果可以,我该怎么做?

My code is as follows:

我的代码如下:

Sub move()
    If ActiveCell.Offset(0, -1) = ActiveCell.Offset(1, -1) Then
        ActiveCell.Formula = "1"
        ActiveCell.Offset(1, 0).Select
    Else
        ActiveCell.Offset(1, 0).Select
    End If
End Sub

回答by

To loop through a fixed number of times use a For...Nextloop.

要循环固定次数,请使用For...Next循环。

In your case it would be as follows (I have done it for 10 loops but you can change the number as you wish)

在您的情况下,它将如下(我已经完成了 10 个循环,但您可以根据需要更改数字)

Sub move()

Dim i

For i = 1 To 10
    If ActiveCell.Offset(0, -1) = ActiveCell.Offset(1, -1) Then
        ActiveCell.Formula = "1"
    End If
    ActiveCell.Offset(1, 0).Select
Next i

End Sub