VBA入门之循环

1.For循环

  1. 已知循环次数
    for i=1 To 10 Step =2
    
        Debug.Print i
    
    Next
    
    'Step是步长,默认不指出的话是1
  2.  循环次数不确定 
    For Each a In myArr
        
        Debug.Print a
    
    Next a

2.Loop循环

  1.  先判断,再执行

    Do [While | Unitl]循环条件 
        
        '语句
    
    Loop
    
    
    Sub test()
    
        Dim i as integer
        i=3
    
        Do while i>0
            Debug.Print i
            i = i-1
        Loop
    End Sub
  2. 先执行再判断
    sub test()
    
        Dim i as integer
        i=3
    
        Do
            Debug.Print i
    
            i = i-1 
        Loop While i >0
    End Sub
     
  3. While循环 
    Sub test()
    
        Dim i as integer
        i=3
    
        While i>0
            Debug.Print i 
            i = i -1 
        Wend
    End Sub
     

猜你喜欢

转载自blog.csdn.net/weixin_33957036/article/details/81199259