美文网首页
简明Excel VBA(二)语句结构

简明Excel VBA(二)语句结构

作者: Bluetata | 来源:发表于2018-12-08 21:16 被阅读0次

简明Excel VBA

本文集同步于GitHub仓库:# Youchien/concise-excel-vba

1.5 语句结构

程序通常都是顺序依次执行的。语句结构用来控制程序执行的步骤,
一般有选择语句、循环语句。

1.5.1 选择语句

选择语句用来判断程序执行那一部分代码

语法:If ... Then ... End If</br>
If选择可以嵌套使用</br>

常用的三种形式:

  1. 普通模式
If 10 > 3 Then
    操作1  ' 执行这一步
End If

' 增加Else和Else If逻辑
If 1 > 2 Then
    操作1
ElseIf 1 = 2 Then
    操作2
Else
    操作3  ' 执行这一步
End If
  1. 嵌套If语句
If 10 > 3 Then
    If 1 > 2 Then
        操作1
    Else
        操作2  ' 执行这一步
    End If
Else
    操作3
End If
  1. Select ... Case ... 多选一,类似于java中的 Switch ... Case ... 语句
Dim Length As Integer
Length = 10
Select Length
    Case Is >= 8
        操作1  ' 执行这一步
    Case Is > 20
        操作2
    Case Else
        操作3
End Select

sample code:

Private Sub switch_demo_Click()
    Dim MyVar As Integer
    MyVar = 1

    Select Case MyVar
        Case 1
            Debug.Print "The Number is the Least Composite Number"
        Case 2
            Debug.Print "The Number is the only Even Prime Number"
        Case 3
            Debug.Print "The Number is the Least Odd Prime Number"
        Case Else
            Debug.Print "Unknown Number"
    End Select
End Sub

1.5.2 循环语句

循环语句用来让程序重复执行某段代码

  1. 普通For ... Next循环</br>
    语法:For 循环变量 = 初始值 To 终值 Step 步长</br>
    注:在VBA循环中可以使用Exit关键字来跳出循环,类似于Java中的break,
    在for循环中语法为:Exit For,在do while循环中为:Exit Do,也可以利用GoTo语句
    跳出本次循环,详见:1.5.3 GoTo语句</br>
Dim i As Integer
For i = 1 To 10 Step 2 ' 设定i从1到10,每次增加2,总共执行5次
    操作1   ' 可以通过设定 Exit For 退出循环
Next i
  1. For Each ... 循环</br>
    语法:For Each 变量 In 集合或数组
Dim arr
Dim i As Integer
arr = Array(1, 2, 3, 4, 5)
For Each i In arr ' 定义变量i,遍历arr数组
    操作1
Next i
  1. Do ... While循环</br>
    语法:</br>
  • 前置循环条件:</br>


    image.png
  • 后置循环条件:</br>


    image.png

Sample code:

Dim i As Integer
i = 1
Do While i < 5  ' 循环5次
    i = i + 1
Loop

' ===============================================
' 将判断条件后置的Do...While
Dim i As Integer
i = 1
Do
    i = i + 1
Loop While i < 5 ' 循环4次
  1. Do Until 直到...循环</br>
    语法:</br>
    Do Until 表达式 表达式为真时跳出循环
Dim i As Integer
i = 5
Do Until i < 1  
    i = i - 1
Loop

' ===============================================
' 后置的Do Until
Dim i As Integer
i = 5
Do
    i = i - 1
Loop Until i < 1  

<a name="1.5.3"></a>

1.5.3 GoTo语句

GoTo
无条件地分支直接跳转到过程中指定的行。

注: GoTo语句大多用于错误处理时,但会影响程序结构,增加阅读和代码调试难度,
除非必要时,应尽量避免使用GoTo语句。

Sub TestGoTo

    Dim lngSum As Long, i As Integer
    i = 1

JUMPX:
    i = i + 1
    If i <= 100 Then GoTo JUMPX
    Debug.Print "1到100的自然数之和是:" & lngSum

End Sub

CONTINUE

循环中实现continue操作,类似java语言的continue直接跳出本次循环

Sub continueTest()
    Dim i

    For i = 0 To 5
        If i = 1 Then
            '// 跳转到CONTINUE部分
            GoTo CONTINUE
        ElseIf i = 3 Then
            '// 跳转到CONTINUE部分
            GoTo CONTINUE
        End If

        '//没有GoTo语句的时候打印counter: i
        Debug.Print i

CONTINUE:   '// countinue跳转块,可以写逻辑,如果没有逻辑就直接进行下次循环
    Next

End Sub

选择循环提供了多种实现同一目的的语句结构,他们都能实现同样的作用,
差别一般是初始条件。还有书写的复杂度。正确的选择要使用的语句结构,
代码逻辑上会更清楚,方便人的阅读。

简写

在操作对象的属性时常常要先把对象调用路径都写出来,用with可以简化这一操作

' 简化前
WorkSheets("表1").Range("A1").Font.Name="仿宋"
WorkSheets("表1").Range("A1").Font.Size=12
WorkSheets("表1").Range("A1").Font.ColorIndex=3

' 使用`With`
With WorkSheets("表1").Range("A1").Font
    .Name = "仿宋"
    .Size = 12
    .ColorIndex =3
End With

相关文章

网友评论

      本文标题:简明Excel VBA(二)语句结构

      本文链接:https://www.haomeiwen.com/subject/godchqtx.html