20160127:开始学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   //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

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 //跳出do循环
   End If
 Loop Until x = 14
End Sub

 

posted on 2016-01-27 23:51  lao_wan  阅读(226)  评论(0编辑  收藏  举报

导航