首页 > 代码库 > VBA基础——循环语句

VBA基础——循环语句

VBA基础之循环语句

Sub s1()
 Dim rg As Range
 For Each rg In Range("a1:b7,d5:e9")
   If rg = "" Then
     rg = 0
   End If
  Next rg
End Sub

Sub s2()
 Dim x As Integer
 Do
   x = x + 1
   If Cells(x + 1, 1) <> Cells(x, 1) + 1 Then
      Cells(x, 2) = "断点"
      Exit Do
   End If
 Loop Until x = 14
End Sub

  

VBA各语句对比

Sub t1()
  Range("d2") = Range("b2") * Range("c2")
  Range("d3") = Range("b3") * Range("c3")
  Range("d4") = Range("b4") * Range("c4")
  Range("d5") = Range("b5") * Range("c5")
  Range("d6") = Range("b6") * Range("c6")
End Sub

Sub t2()
Dim x As Integer
 For x = 10000 To 2 Step -3
  Range("d" & x) = Range("b" & x) * Range("c" & x)
 Next x
End Sub


Sub t3()
Dim rg As Range
 For Each rg In Range("d2:d18")
  rg = rg.Offset(0, -1) * rg.Offset(0, -2)
 Next rg
End Sub


Sub t4()
Dim x As Integer
 x = 1
 Do
   x = x + 1
   Cells(x, 4) = Cells(x, 2) * Cells(x, 3)
 Loop Until x = 18
End Sub

Sub t5()
 x = 1
 Do While x < 18
   x = x + 1
   Cells(x, 4) = Cells(x, 2) * Cells(x, 3)
 Loop
End Sub

  VBA基础循环语句对比

 

VBA基础——循环语句