Vba-arithmetic-operators

提供:Dev Guides
移動先:案内検索

VBA-算術演算子

次の算術演算子はVBAでサポートされています。

変数Aが5を保持し、変数Bが10を保持すると仮定します-

Operator Description Example
PLUS Adds the two operands A PLUS B will give 15
- Subtracts the second operand from the first A - B will give -5
AST Multiplies both the operands A AST B will give 50
/ Divides the numerator by the denominator B/A will give 2
% Modulus operator and the remainder after an integer division B % A will give 0
^ Exponentiation operator B ^ A will give 100000

VBAで使用可能なすべての算術演算子を理解するには、ボタンを追加して次の例を試してください。

Private Sub Constant_demo_Click()
   Dim a As Integer
   a = 5

   Dim b As Integer
   b = 10

   Dim c As Double

   c = a + b
   MsgBox ("Addition Result is " & c)

   c = a - b
   MsgBox ("Subtraction Result is " & c)

   c = a * b
   MsgBox ("Multiplication Result is " & c)

   c = b/a
   MsgBox ("Division Result is " & c)

   c = b Mod a
   MsgBox ("Modulus Result is " & c)

   c = b ^ a
   MsgBox ("Exponentiation Result is " & c)
End Sub

ボタンをクリックするか、上記のスクリプトを実行すると、次の結果が生成されます。

Addition Result is 15

Subtraction Result is -5

Multiplication Result is 50

Division Result is 2

Modulus Result is 0

Exponentiation Result is 100000