VBA基础02:VBA基本语句结构

2019-04-04  本文已影响0人  michaelxwang

选择判断

选择判断(If...Then)

Sub iftime()
    MsgBox Time
    If Time < 0.5 Then
        MsgBox " 早上好!"
    ElseIf Time > 0.75 Then
        MsgBox " 晚上好!"
    Else
        MsgBox " 下午好!"
    End If
End Sub

选择判断(Select...Case)

Sub dengji()
'   定义一个Variant型变量cj
    Dim cj As Variant
    cj = InputBox(" 输入考试成绩:")
'   Select语句开始
    Select Case cj
    Case 0 To 59
        MsgBox " 等级:D "
    Case 60 To 69
        MsgBox " 等级:C "
    Case 70 To 89
        MsgBox " 等级:B "
    Case 90 To 100
        MsgBox " 等级:A "
    Case Else
        MsgBox " 输入错误!"
'   Select 语句结束
    End Select
End Sub

循环

循环(For...Next)

For   <循环变量>=<初值>To<终值>[Step 步长值]
      <循环体>
      [Exit For]
      [循环体]
Next [循环变量]

例:根据A列成绩在B列更新其对应等级

Sub dengji()
    Dim xj As String, i As Integer
    For i = 2 To 50 Step 1
        Select Case Cells(i, "A")
            Case 0 To 59
                xj = " D "
            Case 60 To 69
                xj = " C "
            Case 70 To 89
                xj = " B "
            Case 90 To 100
                xj = " A "
            Case Else
                xj = " 输入错误!"
        End Select
        Cells(i, "B") = xj
    Next i
End Sub
for.gif

循环(Do...While)

Do   [While 逻辑表达式]
     <循环体>
     [Exit Do]
     [循环体]
Loop

例:根据A列成绩在B列更新其对应等级

Sub dengji()
    Dim xj As String, i As Integer
    i = 2
    Do While Cells(i, "A") <> ""
        Select Case Cells(i, "A")
            Case 0 To 59
                xj = " D "
            Case 60 To 69
                xj = " C "
            Case 70 To 89
                xj = " B "
            Case 90 To 100
                xj = " A "
            Case Else
                xj = " 输入错误!"
        End Select
        Cells(i, "B") = xj
    i = i + 1
    Loop
End Sub
DoWhile.gif

也可以在结尾处判断循环条件,语句为:

Do
<循环体>
[Exit Do]
[循环体]
Loop [While 逻辑表达式]

注意: 这种循环格式在循环条件一开始就为false的时候要多执行一次循环体
例:根据A列成绩在B列更新其对应等级

Sub dengji()
    Dim xj As String, i As Integer
    i = 2
    Do
        Select Case Cells(i, "A")
            Case 0 To 59
                xj = " D "
            Case 60 To 69
                xj = " C "
            Case 70 To 89
                xj = " B "
            Case 90 To 100
                xj = " A "
            Case Else
                xj = " 输入错误!"
        End Select
        Cells(i, "B") = xj
    i = i + 1
    Loop While Cells(i, "A") <> ""
End Sub

循环(Do...Until)

Do [Until 逻辑表达式]
      <循环体>
     [Exit Do]
     [循环体]
Loop

结尾判断式

Do 
      <循环体>
     [Exit Do]
     [循环体]
Loop [Until 逻辑表达式]
Sub dengji()
    Dim xj As String, i As Integer
    i = 2
    Do
        Select Case Cells(i, "A")
            Case 0 To 59
                xj = " D "
            Case 60 To 69
                xj = " C "
            Case 70 To 89
                xj = " B "
            Case 90 To 100
                xj = " A "
            Case Else
                xj = " 输入错误!"
        End Select
        Cells(i, "B") = xj
    i = i + 1
    Loop Until Cells(i, "A")  = ""
End Sub

循环(For Each...Next)

For Each 元素变量 In 集合名称或数组名称
    <语句块1>
    [Exit For]
    [语句块2]
next [元素变量]

例:获取工作簿的各个工作表名

Sub shtname()
    Dim shtname()
        Dim sht As Worksheet, i As Integer
        i = 1
        For Each sht In Worksheets
            Cells(i, "D") = sht.Name
            i = i + 1
        Next sht
End Sub
上一篇下一篇

猜你喜欢

热点阅读