zoukankan      html  css  js  c++  java
  • VBA 学习笔记 判断语句、循环

    判断语句

    大部分和 Lua 差不多,多了一个 Switch 语句

    循环

    For 循环

    多次执行一系列语句,缩写管理循环变量的代码。
    For i = start To end [Step X]...Next

    Private Sub Constant_demo_Click()
       Dim a As Integer
       a = 10
    
       For i = 0 To a Step 2
          MsgBox ("The value is i is : " & i)
       Next
    End Sub
    

    For Each 循环

    主要用于数组,如果组中至少有一个元素并为组中的每个元素重复执行,则执行此操作。

    For Each element In Group
       [statement 1]
       [statement 2]
       ....
       [statement n]
       [Exit For]
       [statement 11]
       [statement 22]
    Next
    
    Private Sub Constant_demo_Click()  
       'fruits is an array
       fruits = Array("苹果", "橙子", "樱桃")
       Dim fruitnames As Variant
    
       'iterating using For each loop.
       For Each Item In fruits
          fruitnames = fruitnames & Item & Chr(10)
       Next
    
       MsgBox fruitnames
    End Sub
    

    While ... Wend 循环

    在执行循环体之前测试条件

    Private Sub Constant_demo_Click()
       Dim a As Integer
       a = 10
    
       For i = 0 To a Step 2
          MsgBox ("The value is i is : " & i)
       Next
    End Sub
    

    Do...While循环

    只要条件为 True 就会被执行(即,)循环应该被重复直到条件为 False。
    相当于 Lua 中的 while true do ... end

    Private Sub Constant_demo_Click()
       Do While i < 5
          i = i + 1
          msgbox "The value of i is : " & i
       Loop
    End Sub
    

    do…until 循环

    只要条件是 False,do..Until 语句就会被执行(即,)循环应该被重复直到条件为真。

    循环控制

    相当于 Lua 中的 break

    Exit For 语句

    Private Sub Constant_demo_Click()
       Dim a As Integer
       a = 10
    
       For i = 0 To a Step 2 
    'i is the counter variable and it is incremented by 2
          MsgBox ("The value is i is : " & i)
          If i = 4 Then
             i = i * 10 
    'This is executed only if i=4
             MsgBox ("The value is i is : " & i)
             Exit For 'Exited when i=4
          End If
       Next
    End Sub
    

    Exit Do 语句

    Private Sub Constant_demo_Click()
       i = 0
       Do While i <= 100
          If i > 10 Then
             Exit Do   ' Loop Exits if i>10
          End If
          MsgBox ("The Value of i is : " & i)
          i = i + 2
       Loop
    End Sub
    
  • 相关阅读:
    基于CSS 和JS的网页SELECT下拉框美化,JQUERY 插件
    ini文件读写
    Hibernate 表关系描述之OneToMany
    Hibernate 表关系描述之ManyToMany
    Hibernate 初识
    Hibernate 表关系描述之OneToOne
    Struts配置文件初解
    Hibernate 初识(补充)
    struts 学习之04 "模型"
    (Struts)Action类及其相关类
  • 原文地址:https://www.cnblogs.com/lisaisacat/p/12322615.html
Copyright © 2011-2022 走看看