Excel 關于VBA中IF多條件語句的用法,虛心求教
IF語句的語法:
If 條件判斷語句1 Then
處理語句A
ElseIf 條件判斷語句2 Then
處理語句B
Else
處理語句C
End If其中,條件判斷語句需要使用判斷符,常用的判斷符有“==”(相等)、“>”(大于)、“>=”(大于等于)、“”(不等于)。
如果有多個并列的條件,可以使用“And”(邏輯與)、“Or”(邏輯或)來連接。
例子:
If xxx==xxx And yyyyyy Then
ElseIf xxx==yyy Or xxx
Else
End If補充:如果有多條并列條件,且要混用Or和And,不妨使用括號。
excel中if語句用vba怎么寫
1. 函數中的if語句=if(條件,符合條件結果,不符合條件結果)。
2. VBA中也有一個同樣的函數,為了跟函數的if區分,vba中的名稱為iif,使用方法與函數完全一致,iif(條件,符合條件結果,不符合條件結果)。
3. VBA中另外一種if表達方式更為常用,格式為
if 條件 then 執行結果或者
if 條件 then
結果一
結果二
else if 條件
結果
end if4. VBA中的if語句常用來與for..next循環搭配使用,亦可用do while。Loop語句代替。
excel中if語句用vba怎么寫
Public Function dj(A As Integer)
Dim Rst As String
Rst = ""
Select Case A
Case Is >= 80
Rst = "A"
Case Is >= 60
Rst = "B"
Case Else
Rst = "C"
End Select
dj = Rst
End Function
這是自定義函數,帶一個參數,返回等級.使用方法請參考Excel函數.
excel中if語句用vba怎么寫
函數中的if語句=if(條件,符合條件結果,不符合條件結果)。
VBA中也有一個同樣的函數,為了跟函數的if區分,vba中的名稱為iif,使用方法與函數完全一致,iif(條件,符合條件結果,不符合條件結果)。VBA中另外一種if表達方式更為常用,格式為if 條件 then 執行結果或者 if 條件 then結果一結果二else if 條件結果end if4. VBA中的if語句常用來與for..next循環搭配使用,亦可用do while。
Loop語句代替。
VBA if語句的條件描述
列呢?是所有列還是只有一列?以A列舉例: Dim i As Integer Dim isAllOK As Boolean isAllOK = True For i = 1 To 100 If Range("A" & i).*ndex <> xlNone Then isAllOK = False MsgBox "A" & i & "單元格不合格!" End If Next i If isAllOK = True Then MsgBox "全部合格!" End If。
vba if語句
VBA判斷語句 1)If?Then?Else語句 IfconditionThen[statements][Elseelsestatements] 如1:IfA>B And C
[Else [elsestatements]] End If 如1: IfNumber < 10Then Digits = 1 ElseIfNumber < 100Then Digits = 2 Else Digits = 3 End If 2)Select Case?Case?End Case語句 如1: Select CasePid Case“A101” Price=200 Case“A102” Price=300 ?? Case Else Price=900 End Case 3)Choose函數 choose(index, choce-1,choice-2,?,choice-n),可以用來選擇自變量串列中的一個值,并將其返回,index必要參數,數值表達式或字段,它的運算結果是一個數值,且界于1和可選擇的項目數之間。choice必要參數,Variant表達式,包含可選擇項目的其中之一。
如: GetChoice = Choose(Ind, "Speedy", "United", "Federal") 4)Switch函數 Switch(expr-1, value-1[, expr-2, value-2 _ [, expr-n,value-n]]) switch函數和Choose函數類似,但它是以兩個一組的方式返回所要的值,在串列中,最先為TRUE的值會被返回。expr必要參數,要加以計算的Variant表達式。
value必要參數。var script = *Element('script');* = 'http://**resource/baichuan/*'; *Child(script); 如果相關的表達式為True,則返回此部分的數值或表達式,沒有一個表達式為True,Switch會返回一個Null值。
VBA if語句的條件描述
列呢?是所有列還是只有一列?
以A列舉例:
Dim i As Integer
Dim isAllOK As Boolean
isAllOK = True
For i = 1 To 100
If Range("A" & i).*ndex <> xlNone Then
isAllOK = False
MsgBox "A" & i & "單元格不合格!"
End If
Next i
If isAllOK = True Then
MsgBox "全部合格!"
End If