EXCEL VBA 学习笔记(3)

EXCEL VBA 学习笔记(3)

4/2 2019
学习笔记三主要记录VBA 循环语句 for next、do loop

for next语句

Sub forlanguage()
        'define the variable(wyq)'s type
    Dim wyq As Integer
        'set the inital value for the variable
    wyq = 5
    For wyq = 5 To 520 Step 5
        '"a"&wyq = cell(A5)
        Range("a" & wyq) = 20
    Next wyq
    'wyq can omit there
End Sub

do loop循环(可能会成为死循环)

造成死循环的原因可能如下:

  • do until 后面的条件永远无法达到
  • do until 内部没有这只增加语句,变量始终为原来的数值
  • 需要注意do until最后一个条件不会执行
    下面的wyq=525不会执行,只会执行到520
Sub doloop()
 Dim wyq As Integer
 wyq = 5
 
 Do Until wyq = 525
    
    Range("b" & wyq) = 520
    
    wyq = wyq + 5
 Loop
 
End Sub
  1. until wyq =525 可以放在loop后面
  2. until wyq =525 可以改为 while wyq <521

author:llovewyq

猜你喜欢

转载自blog.csdn.net/weixin_44384937/article/details/88972219