全民一起VBA基础篇第四课:基本流程控制语句

for语句

Sub toRMB()
    Dim rate, i
    
    If Cells(7, 6) = "USD" Then         '做一个简单的if判断
    
        rate = Cells(8, 6)
        
        For i = 11 To 20 Step 1         '步长为1可省略,若是20 to 11,则step -1
            Cells(i, 6) = Cells(i, 6) * rate
        Next i                          '保持良好的代码风格,利用tab键缩进
        
        Cells(7, 6) = "RMB"
        
    End If
    
End Sub

在这里插入图片描述

Option Explicit
Sub 做加法()
Dim i As Integer

    For i = 2 To 28 Step 1
        Cells(i, 6) = Cells(i, 2) + Cells(i, 4)
    Next i

End Sub

在这里插入图片描述

if语句

Option Explicit
Sub calculate()
Dim score
Dim i As Integer

score = 0

For i = 4 To 6 Step 1
    score = Cells(i, 6) + score         '简单求和
Next i

score = score / 3
Cells(7, 6) = score                     'if-elseif判断,elseif是一个整体
If score >= 90 Then
    Cells(8, 6) = "A"
ElseIf score >= 80 Then                 'then不能忘掉
    Cells(8, 6) = "B"
ElseIf score >= 70 Then
    Cells(8, 6) = "C"
ElseIf score >= 60 Then
    Cells(8, 6) = "D"
Else
    Cells(8, 6) = "Fail"
End If                                  'endif 不能少

End Sub

在这里插入图片描述

发布了26 篇原创文章 · 获赞 5 · 访问量 1107

猜你喜欢

转载自blog.csdn.net/qq_43568982/article/details/103742017