TOP
VBA-第 8.2 课。循环 (Loops)
为了练习我们刚刚学到的内容,我们将逐步完成创建宏的过程,以从当前活动的单元格开始绘制 10x10 棋盘(红色和黑色)。
让我们看看我们想要得到的循环的输出:
接下来,练习的第一步是创建一个空过程:
- Sub loops_exercise()
-
- Const NB_CELLS As Integer = 10
-
-
-
- End Sub
Sub loops_exercise()
Const NB_CELLS As Integer = 10 '我们想要绘制的单元格数量
'...
End Sub
首先添加一个 FOR 循环来将 A 列涂黑(NB_CELLS 常量为 10)。
我们应该得到以下结果:
我们看一下代码:
- Sub loops_exercise()
-
- Const NB_CELLS As Integer = 10
-
- For r = 1 To NB_CELLS
-
- Cells(r, 1).Interior.Color = RGB(0, 0, 0)
-
- Next
-
- End Sub
Sub loops_exercise()
Const NB_CELLS As Integer = 10 '我们想要绘制的单元格数量
For r = 1 To NB_CELLS 'r => 行号
Cells(r, 1).Interior.Color = RGB(0, 0, 0) '黑色的
Next
End Sub
下一步使用 IF 指令将每个后续单元格着色为红色(基于行号是偶数还是奇数)。看降低:
解决该子任务的代码如下:
- Sub loops_exercise()
-
- Const NB_CELLS As Integer = 10
-
- For r = 1 To NB_CELLS
-
- If r Mod 2 = 0 Then
- Cells(r, 1).Interior.Color = RGB(200, 0, 0)
- Else
- Cells(r, 1).Interior.Color = RGB(0, 0, 0)
- End If
-
- Next
-
- End Sub
Sub loops_exercise()
Const NB_CELLS As Integer = 10 '我们想要绘制的单元格数量
For r = 1 To NB_CELLS 'r => 行号
If r Mod 2 = 0 Then 'Mod => 是除法的余数
Cells(r, 1).Interior.Color = RGB(200, 0, 0) '红色的
Else
Cells(r, 1).Interior.Color = RGB(0, 0, 0) '黑色的
End If
Next
End Sub
条件 IF r Mod 2 = 0 表示:如果 r 除以 2 的余数为 0...
只有偶数的行号除以 2 时余数为 0:
如何创建另一个循环来执行我们已经为 10 列编写的循环。看降低:
解决该子任务的代码如下:
- Sub loops_exercise()
-
- Const NB_CELLS As Integer = 10
-
- For r = 1 To NB_CELLS
-
- For c = 1 To NB_CELLS
-
- If r Mod 2 = 0 Then
- Cells(r, c).Interior.Color = RGB(200, 0, 0)
- Else
- Cells(r, c).Interior.Color = RGB(0, 0, 0)
- End If
-
- Next
- Next
-
- End Sub
Sub loops_exercise()
Const NB_CELLS As Integer = 10 '带单元格的 10x10 棋盘
For r = 1 To NB_CELLS 'r => 行号
For c = 1 To NB_CELLS 'c => 列号
If r Mod 2 = 0 Then
Cells(r, c).Interior.Color = RGB(200, 0, 0) '红色的
Else
Cells(r, c).Interior.Color = RGB(0, 0, 0) '黑色的
End If
Next
Next
End Sub
现在第二个循环被插入到第一个循环中。
为了达到以下结果...
代替:
If r Mod 2 = 0 Then
在:
- If (r + c) Mod 2 = 0 Then
If (r + c) Mod 2 = 0 Then
剩下要做的就是更改代码,以便从选定的单元格(而不是 A1)开始创建棋盘。见下文:
为此,我们将编写以下代码:
- Sub loops_exercise()
-
- Const NB_CELLS As Integer = 10
- Dim offset_row As Integer, offset_col As Integer
-
-
- offset_row = ActiveCell.Row - 1
-
- offset_col = ActiveCell.Column - 1
-
- For r = 1 To NB_CELLS
-
- For c = 1 To NB_CELLS
-
- If (r + c) Mod 2 = 0 Then
-
-
- Cells(r + offset_row, c + offset_col).Interior.Color = RGB(200, 0, 0)
- Else
- Cells(r + offset_row, c + offset_col).Interior.Color = RGB(0, 0, 0)
- End If
-
- Next
- Next
-
- End Sub
Sub loops_exercise()
Const NB_CELLS As Integer = 10 '带单元格的 10x10 棋盘
Dim offset_row As Integer, offset_col As Integer '=>添加2个变量
'从第一个单元格开始的偏移量(行) = 当前活动单元格的行号 - 1
offset_row = ActiveCell.Row - 1
'从第一个单元格开始的偏移量(列) = 当前活动单元格的列号 - 1
offset_col = ActiveCell.Column - 1
For r = 1 To NB_CELLS '电话号码
For c = 1 To NB_CELLS '列数
If (r + c) Mod 2 = 0 Then
'单元格(行号+附加行偏移量,列号+
'扬声器的额外位移)
Cells(r + offset_row, c + offset_col).Interior.Color = RGB(200, 0, 0) '红色的
Else
Cells(r + offset_row, c + offset_col).Interior.Color = RGB(0, 0, 0) '黑色的
End If
Next
Next
End Sub