VBA 中的 To 和 Step 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19687018/
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-08 17:04:27  来源:igfitidea点击:

What does the To and Step mean in VBA?

vba

提问by usernolongerregistered

Dim i As Long
Dim rows As Long
Dim rng3 As Range
rows = rng3.rows.Count
For i = rows To 1 Step (-1)

Does anyone know how this loop works? I'm confused on the meaning of rows To 1 Step (-1).

有谁知道这个循环是如何工作的?我对 的含义感到困惑rows To 1 Step (-1)

回答by

from high numberTo 1adding(-1)each iteration

高数1添加(-1)每次迭代

Note:It's addingbecause +AND -in mathematical logic evaluate to a -

注:它的加入,因为+-数理逻辑计算为-



If rows = 10then

如果rows = 10那么

for i = 10 to 1 step -2would mean loop back from 10 to 1subtracting2from the iin each loop cycle.

for i = 10 to 1 step -2将意味着从环回10 to 1减去2i每个循环周期

adding a Debug.Print iinside the loop may give you a better clue.

Debug.Print i在循环内添加 a可能会给你一个更好的线索。

Note: turn ON the Immediate Windowhitting CTRL+Gor View => Immediate Windowfrom the VBE menu bar

注意:从 VBE 菜单栏中打开Immediate Window点击CTRL+GView => Immediate Window

enter image description here

在此处输入图片说明



An example loop increasing by 3 on each cycle.

每个循环增加 3 的示例循环。

for i = 1 to 10 step 3
    debug.print i 
next i

enter image description here

在此处输入图片说明



Usage

用法

The step-back technique is mostly used when deleting rows from a spreadsheet.

从电子表格中删除行时主要使用后退技术。

To see the logic in practice see the following

要查看实践中的逻辑,请参阅以下内容

回答by Sam

When deleting rows, it is often common practise to start at the end and step backwards, this is so no rows are skipped.

删除行时,通常的做法是从末尾开始并后退,这样就不会跳过任何行。

Dim i As Long
Dim rows As Long
Dim rng3 As Range

rows = rng3.rows.Count


For i = rows To 1 Step (-1)

    'delete row if "delete" is in column 1
    If rng3.cells(i,1).Value = "delete" Then

    rng3.Rows(i).EntireRow.Delete

    End If

next i

回答by DHARMRAJSINGH

Dim i as Integer

For i = 1 To 14 Step 3
Debug.Print i
Next i

In above code loop will iterate from 1 to 14 increment with 3 so output will be like

在上面的代码循环将从 1 到 14 以 3 为增量进行迭代,因此输出将类似于

1 4 7 10 13

It means it can not cross 14 that is limit.

这意味着它不能越过14,即限制。

So whatever value is provided in step it will add into the variable use for looping purpose. Here

因此,无论在 step 中提供什么值,它都会添加到用于循环目的的变量中。这里

i = i +3

But in For loop in VBA, Step value can not be changed dynamically. For example:

但是在 VBA 的 For 循环中,Step 值不能动态改变。例如:

Dim i As Integer

For i = 1 To 10 Step i
    Debug.Print i
Next i

Here, before starting iteration Step is equal to the value of ithat is the default value i.e. 0. So i will increment like below:

在这里,在开始迭代之前 Step 等于 i的值,即默认值 0。所以 i 将增加如下:

i = i+ i => i = i+0

So i will not increment here and loop will iterate for ever.

所以我不会在这里递增,循环将永远迭代。

Now for below code:

现在为以下代码:

Dim i as Integer

For i = 1 To 14 Step i+1
Debug.Print i
Next i

i will increment like :

我会像这样递增:

i=i+(i+1) => i= i+(0+1) =>i = i+1

so it will increment by 1 and output will be 1 2 3 .... 14

所以它将增加 1 并且输出将是 1 2 3 .... 14

Now for below code :

现在对于下面的代码:

Dim i As Integer
i = 3
For i = 1 To 10 Step i
    Debug.Print i
Next i

here, i is equal to 3 before loop execution, so Step value will be 3, but loop will start with i = 1 and will increment with 3 through out the loop. here,

在这里,在循环执行之前 i 等于 3,因此 Step 值将为 3,但循环将从 i = 1 开始,并在整个循环中以 3 递增。这里,

i = i+3

so output will be 1 4 7 10.

所以输出将是 1 4 7 10。

Now for some other variable:

现在对于一些其他变量:

Dim i As Integer
Dim j As Integer
j = 2

For i = 1 To 10 Step j
    Debug.Print i
    j = i
Next i

in above code Step value will be 2, so i will increment by 2 for every iteration whether j is modifying inside loop or not, it will not impact Step value, so output will be

在上面的代码中,Step 值为 2,所以无论 j 是否在循环内部修改,每次迭代我都会增加 2,它不会影响 Step 值,所以输出将是

1 3 5 7 9

Please correct me if I miss anything or something is wrong in this. Also suggest if there is any way for dynamic looping using For loop in VBA.

如果我遗漏了任何内容或有什么问题,请纠正我。还建议是否有任何方法可以在 VBA 中使用 For 循环进行动态循环。