Excel 中的 VBA 代表Visual Basic for Applications ,它是 Microsoft 的编程语言。为了优化性能并减少在 Excel 中的时间,我们需要宏,而 VBA 是后端使用的工具。
一些有用的链接,以获取有关 Excel 中宏、VBA 的更多见解:
1. 在 Excel 中记录宏。
2. 如何在 Excel 中创建宏?
在这篇文章中,我们将在Excel VBA中讨论各种比较运算符。
执行 :
在 Microsoft Excel 选项卡中,选择开发人员选项卡。最初,开发人员选项卡可能不可用。
开发人员选项卡可以通过两步过程轻松启用:
- 右键单击 Excel 窗口顶部的任何现有选项卡。
- 现在从弹出菜单中选择自定义功能区。
- Excel 选项框,选中开发人员框以启用它,然后单击确定。
- 现在,开发人员选项卡可见。
- 现在单击 Developer 选项卡中的Visual Basic选项并创建一个新模块来编写程序。
Developer -> Visual Basic -> Tools -> Macros
现在创建一个宏并给出任何合适的名称。
这将打开编辑器窗口,可以在其中编写代码。
Excel 中的比较运算符:
S.No. | Operators |
Definition |
---|---|---|
1 | <> |
Not equal operator is used to compare two operands. If the two operands are not equal it returns TRUE else it returns FALSE. For example : A=10, B= 20 The condition will be TRUE for A <> B, since A and B are not equal. |
2 | = |
Equal operator is used to compare two operands. If the two operands are equal it returns TRUE else it returns FALSE. For example : A=20, B= 20 The condition will be TRUE for A = B, since A and B are equal. |
3 | > |
Greater than operator checks whether the operand on the left hand side is strictly greater than the operand on RHS. If greater then it returns TRUE else FALSE. For example : A=10, B= 5 The condition will be TRUE for A > B, since A is greater than B. |
4 | < |
Less than operator checks whether the operand on the left hand side is strictly less than the operand on RHS. If greater then it returns TRUE else FALSE. For example : A=10, B= 5 The condition will be FALSE for A < B, since A is greater than B. |
5 | >= |
Greater than equal to operator checks whether the operand on the left hand side is either greater or equal to the operand on RHS. If greater or equal then it returns TRUE else FALSE. For example : A=10, B=10 The condition will be TRUE for A >= B, since A is equal to B. |
6 | <= |
Less than equal to operator checks whether the operand on the left hand side is either lesser or equal to the operand on RHS. If lesser or equal then it returns TRUE else FALSE. For example : A=5, B=10 The condition will be TRUE for A <= B, since A is less than B. |
如果条件得到满足和FALSE如果没有比较运算符大多用在Excel中的if else Then语句使用,因为比较运算符返回TRUE。
Excel 中 If Else 的语法是:
If condition/expression Then
Code Block for True
Else
Code Block for False
End If
让我们的示例,其中A = -1和B =的值-5,看看在Excel VBA的代码对于所有的运算符。
1.等于和不等于
Sub Comparison_Operator_Demo()
'Entering the numbers
Dim A As Integer: A = -1
Dim B As Integer: B = -5
'Condition for Equal To
If A = B Then
MsgBox " A and B are equal"
Else
MsgBox " A and B are not equal"
End If
End Sub
在上面的代码中,如果条件变为FALSE,因为 A 和 B 值不同。所以Else里面的代码块会被执行。
Sub Comparison_Operator_Demo()
'Entering the numbers
Dim A As Integer: A = -1
Dim B As Integer: B = -5
'Condition for Not Equal To
If A <> B Then
MsgBox " True since A and B are not same"
Else
MsgBox " False since A and B are same"
End If
End Sub
在上面的代码中,如果条件变为TRUE,因为 A 和 B 不相同。所以If里面的代码块会被执行。
2. 大于或小于运算符:
Sub Comparison_Operator_Demo()
'Entering the numbers
Dim A As Integer: A = -1
Dim B As Integer: B = -5
'Condition for Greater Than
If A > B Then
MsgBox " A is greater than B"
Else
MsgBox " A is not greater than B"
End If
End Sub
在上面的代码中,如果条件变为TRUE,因为 A 大于 B。因此将执行 If 中的代码块。
Sub Comparison_Operator_Demo()
'Entering the numbers
Dim A As Integer: A = -1
Dim B As Integer: B = -5
'Condition for Less Than
If A < B Then
MsgBox "True because A is less than B"
Else
MsgBox "False because A is greater than B"
End If
End Sub
在上面的代码中,如果条件变为FALSE,因为 A 大于 B。因此将执行 Else 中的代码块。
同样,您可以对大于等于和小于等于运算符。