- VBA 教程
- VBA - 首頁
- VBA - 概述
- VBA - Excel 宏
- VBA - Excel 術語
- VBA - 宏註釋
- VBA - 訊息框
- VBA - 輸入框
- VBA - 變數
- VBA - 常量
- VBA - 運算子
- VBA - 決策
- VBA - 迴圈
- VBA - 字串
- VBA - 日期和時間
- VBA - 陣列
- VBA - 函式
- VBA - 子過程
- VBA - 事件
- VBA - 錯誤處理
- VBA - Excel 物件
- VBA - 文字檔案
- VBA - 繪製圖表
- VBA - 使用者窗體
- VBA 有用資源
- VBA - 快速指南
- VBA - 有用資源
- VBA - 討論
VBA - 邏輯運算子
VBA 支援以下邏輯運算子。
假設變數 A 為 10,變數 B 為 0,則:
| 運算子 | 描述 | 示例 |
|---|---|---|
| AND | 稱為邏輯 AND 運算子。如果兩個條件都為真,則表示式為真。 | a<>0 AND b<>0 為假。 |
| OR | 稱為邏輯 OR 運算子。如果兩個條件中的任何一個為真,則條件為真。 | a<>0 OR b<>0 為真。 |
| NOT | 稱為邏輯 NOT 運算子。用於反轉其運算元的邏輯狀態。如果一個條件為真,則邏輯 NOT 運算子將使其為假。 | NOT(a<>0 OR b<>0) 為假。 |
| XOR | 稱為邏輯異或。它是 NOT 和 OR 運算子的組合。如果只有一個表示式計算結果為真,則結果為真。 | (a<>0 XOR b<>0) 為真。 |
示例
嘗試以下示例,透過建立一個按鈕並新增以下函式來了解 VBA 中所有可用的邏輯運算子。
Private Sub Constant_demo_Click()
Dim a As Integer
a = 10
Dim b As Integer
b = 0
If a <> 0 And b <> 0 Then
MsgBox ("AND Operator Result is : True")
Else
MsgBox ("AND Operator Result is : False")
End If
If a <> 0 Or b <> 0 Then
MsgBox ("OR Operator Result is : True")
Else
MsgBox ("OR Operator Result is : False")
End If
If Not (a <> 0 Or b <> 0) Then
MsgBox ("NOT Operator Result is : True")
Else
MsgBox ("NOT Operator Result is : False")
End If
If (a <> 0 Xor b <> 0) Then
MsgBox ("XOR Operator Result is : True")
Else
MsgBox ("XOR Operator Result is : False")
End If
End Sub
當您將其儲存為 .html 檔案並在 Internet Explorer 中執行它時,上述指令碼將產生以下結果。
AND Operator Result is : False OR Operator Result is : True NOT Operator Result is : False XOR Operator Result is : True
vba_operators.htm
廣告
