vba if语句

if判断有三种格式

 

Sub Test()
    Dim the_value
    the_value = ThisWorkbook.Sheets("Sheet1").Range("A1").Value
End Sub

 

1.if...endif

    With ThisWorkbook.Sheets("Sheet1")
        If the_value = "1" Then
            MsgBox (the_value)
        End If
    End With

 

2.if...else...endif

    With ThisWorkbook.Sheets("Sheet1")
        If the_value = "1" Then
            MsgBox (the_value)
        Else
            MsgBox (the_value)
        End If
    End With

3.if...elseif...else...endif

    With ThisWorkbook.Sheets("Sheet1")
        If the_value = "1" Then
            MsgBox (the_value)
        ElseIf the_value = "2" Then
            MsgBox (the_value)
        ElseIf the_value = "3" Then
            MsgBox (the_value)
        Else
            MsgBox (the_value)
        End If
    End With

 

posted @ 2023-03-06 14:45  绮丽梦境  阅读(220)  评论(0编辑  收藏  举报