for循環(huán)
Sub ahole1()
Dim x As Integer
For x = 2 To 10 step 1
Range("d" & x) = Range("b" & x) * Range("c" & x)
Next x
End Sub
另外一種循環(huán)模式
Sub ahole3()
Dim rg As Range
For Each rg In Range("e2:e10")
rg = rg.Offset(0, -3) * rg.Offset(0, -2)
Next rg
End Sub
什么時(shí)候用for x = 2 to 10? step, 什么時(shí)候用for each rg in,取決于是否能找出數(shù)字規(guī)律,比如有斷點(diǎn)的情況火架,這個(gè)時(shí)候就要用for each in
tips:給空白格賦值為0的代碼
Sub ahole4()
Dim rg As Range
For Each rg In Range("a15:b20,d17:e20")
If rg = "" Then
rg = 0
End If
Next rg
End Sub
注意梗逮,先end if 在進(jìn)入下一個(gè)rg循環(huán)婿斥,("a15:b20,d17:e20") 不規(guī)則區(qū)域表示法
查找斷點(diǎn)代碼
do循環(huán)
do循環(huán)容易造成死循環(huán)
Sub ahole5()
Dim x As Integer
x = 1
Do
x = x + 1
Range("f" & x) = Range("b" & x) * Range("b" & x)
Loop Until x = 9
End Sub
這段代碼顯示錯(cuò)誤13奇钞,我并沒有找到原因龟梦。正確代碼如下
Sub ahole5()
Dim x As Integer
x = 1
Do
x = x + 1
Cells(x, 6) = Cells(x, 2) * Cells(x, 3)
Loop Until x = 9
End Sub