在本文中,我们介绍了VBA中使用的各种循环以及如何使用它们以不同的方式完成相同的任务。

为什么循环?

循环是跨多种编程语言使用的最强大的编程技术之一。循环用于将代码块重复所需的次数,或者直到给定条件的计算结果为true或达到特定值为止,然后执行下一个代码块。

Excel VBA循环的目的是使Excel重复一段代码一定次数。可以指定一个代码必须重复多少次作为一个固定数字(例如,重复执行10次)或一个变量(例如,将重复次数与数据行的次数一样多)。

可以按照不同的方式构造Excel循环以适应不同的情况。通常,可以通过不同的方式来获得相同的结果,以适合您的个人喜好。

Excel VBA中提供三种不同类型的循环:

1.做直到循环2.做循环3.循环{空} 1。 DO UNTIL循环DO UNTIL循环用于无限期地重复代码块,直到将指定条件设置为True。可以在循环的开始或结束时检查条件。 DO UNTIL…LOOP语句在开始时测试条件,而DO…LOOP UNTIL语句在循环末尾测试条件。

DO UNTIL…LOOP语句

的语法直到[条件]

[要重复的代码块]

循环

DO…LOOP UNTIL语句

的语法做 [要重复的代码块]

循环直到[条件]

我们以一个例子解释了DO…UNTIL循环。 Loop1和Loop2宏用于使用DO…UNTIL循环来计算A列和B列中数字的平均值。

样本数据的范围为A15:B27。

Sample1

|列A包含第1轮的分数,列B包含第2轮的分数。我们要计算列C的第1轮和第2轮的分数平均值。在Loop1宏中,我们使用了“ FormulaR1C1”在活动单元格中插入平均公式。在循环末尾检查DO UNTIL循环中的条件语句。

在Loop2宏中,我们使用“ WorksheetFunction.Average”在活动单元格中插入平均值。即使在此宏中,条件语句也会在循环结束时进行检查。

Loop1和Loop2宏之间的唯一区别是Loop1插入了平均值公式,而Loop2计算了平均值,然后在活动单元格中插入了平均值。

OutputSample1

{空} 2。 DO WHILE循环DO WHILE循环用于无限次重复代码块,而指定条件继续为True,并在条件返回False时停止。可以在循环的开始或结尾检查条件。 DO WHILE…LOOP语句在开始时测试条件,而DO…LOOP WHILE语句在循环结束时测试条件。当我们希望循环在检查条件之前至少运行一次代码块时,使用DO…LOOP WHILE语句。

DO WHILE…LOOP语句

的语法[条件]

[要重复的代码块]

循环

DO…LOOP WHILE语句

的语法做 [要重复的代码块]

[条件]时循环

在本示例中,Loop3和Loop4宏用于计算A列和B列单元格中值的平均值。这两个宏都与宏Loop1和Loop2使用相同的样本数据。两者都使用DO WHILE语句来遍历包含数据的范围。

Loop3和Loop4宏之间的唯一区别是它们是表示DO WHILE循环条件的不同方式。

由于Loop3和Loop4宏使用相同的输入数据,甚至执行与Loop1宏相同的功能,因此返回的输出也将与Loop1宏相同。

3。 FOR循环For循环用于将代码块重复特定的次数。

FOR循环的语法

对于count_variable = start_value到end_value {空} [代码块]

下一个count_variable Loop5宏显示了如何使用FOR循环来计算平均值。它还使用与其他宏相同的示例数据。我们使用15作为起始值,因为示例数据从第15 ^行开始。我们使用Range(“ A”&Cells.Rows.Count).End(xlUp).Row查找包含数据的最后一行。 FOR循环将重复(lastcell-15)次。

运行Loop5宏后返回的输出与Loop1宏相同。

仅当要运行平均值的活动单元格为空之前,创建Loop6宏才能计算平均值。

该宏的样本数据在E15至G27范围内。

Sample2

我们使用DO…LOOP WHILE在定义的范围内循环。 IF语句用于检查要插入函数的单元格是否包含值。仅当该宏为空时,它才会将平均值函数插入该单元格。

OutputSample2

Loop7宏还用于计算平均值。在评估是否再次循环之前,它将检查helper列中的值。它还检查平均函数中要使用的单元格引用是否为空。

用于Loop7宏的示例数据在J15:M27范围内。

Sample3

M列用作辅助列。仅当M列中的单元格不为空时,此宏才会插入平均值函数。此宏在插入平均值函数之前检查单元格应该为空。如果平均值函数中引用的单元格为空,则不会插入平均值函数。

OutputSample3

请遵循以下代码

Option Explicit

Sub Loop1()

'Calculating average

'Do Until loop will loop until cell in the previous column of active cell is empty

Range("C15").Select

Do

'Assigning average function on value in cells of previous two consecutive columns

ActiveCell.FormulaR1C1 = "=Average(RC[-1],RC[-2])"

'Moving to cell in next row

ActiveCell.Offset(1, 0).Select

'Checking whether value in cell of previous column is empty

'Do Until loop will loop until condition statement returns True

Loop Until IsEmpty(ActiveCell.Offset(0, -1))

Range("A15").Select

End Sub

Sub Loop2()

'Calculating average

'Do Until loop will loop until cell in the previous column of active cell is empty

'This macro is similar to macro Loop1, only way of calculating average is different

Range("C15").Select

Do

'Worsheet.Average function is used for calculating the average

ActiveCell.Value = WorksheetFunction.Average(ActiveCell.Offset(0, -1).Value, _

ActiveCell.Offset(0, -2).Value)

ActiveCell.Offset(1, 0).Select

Loop Until IsEmpty(ActiveCell.Offset(0, -1))

Range("A15").Select

End Sub

Sub Loop3()

'Calculating average

'Do While loop will run until cell in the previous column of active cell is empty

Range("C15").Select

'Checking whether value in cell of previous column is empty

'Do While loop will loop until condition statement is True

Do While IsEmpty(ActiveCell.Offset(0, -1)) = False

'Assigning average function on value in cells of previous two consecutive column

ActiveCell.FormulaR1C1 = "=Average(RC[-1],RC[-2])"

'Moving to cell in next row

ActiveCell.Offset(1, 0).Select

Loop

Range("A15").Select

End Sub

Sub Loop4()

'Calculating average

'Do While loop will run until cell in the previous column of active cell is empty

'This macro is similar to macro Loop3, only way of applying condition is different

Range("C15").Select

Do While Not IsEmpty(ActiveCell.Offset(0, -1))

ActiveCell.FormulaR1C1 = "=Average(RC[-1],RC[-2])"

ActiveCell.Offset(1, 0).Select

Loop

Range("A15").Select

End Sub

Sub Loop5()

'FOR loop repeats for a fixed number of times determined by the number of rows

Dim i, lastcell As Long

'Finding the last row containing data in column A

lastcell = Range("A" & Cells.Rows.Count).End(xlUp).Row

Range("C15").Select

'i variable is assigned value of 15 as our sample data begin from 15th row

'FOR Loop will loop x

For i = 15 To lastcell

ActiveCell.FormulaR1C1 = "=Average(RC[-1],RC[-2])"

ActiveCell.Offset(1, 0).Select

Next i

Range("A15").Select

End Sub

Sub Loop6()

'Calculating average

'Do Until loop will loop until cell in the previous column of active cell is empty

'It does not calculate an average if there is already something in the cell

Range("G15").Select

Do

If IsEmpty(ActiveCell) Then

ActiveCell.FormulaR1C1 = "=Average(RC[-1],RC[-2])"

End If

ActiveCell.Offset(1, 0).Select

Loop Until IsEmpty(ActiveCell.Offset(0, -1))

Range("E15").Select

End Sub

Sub Loop7()

'Do Until loop runs as long as there is something in cell in next column

'It does not calculate an average if there is already something in the active cell

'Nor if there is no data in cells which are used within average function (to avoid #DIV/0 errors).

'Calculating average

Range("L15").Select

Do

If IsEmpty(ActiveCell) Then

If IsEmpty(ActiveCell.Offset(0, -1)) And IsEmpty(ActiveCell.Offset(0, -2)) Then

ActiveCell.Value = ""

Else

ActiveCell.FormulaR1C1 = "=Average(RC[-1],RC[-2])"

End If

End If

ActiveCell.Offset(1, 0).Select

Loop Until IsEmpty(ActiveCell.Offset(0, 1))

Range("J15").Select

End Sub

如果您喜欢此博客,请在Facebook和Facebook上与您的朋友分享。

我们希望收到您的来信,请让我们知道如何改善我们的工作并为您做得更好。写信给我们[email protected]