Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Working with Operators | VBA基礎
ビジネス自動化のためのExcel VBA

Working with Operators

メニューを表示するにはスワイプしてください

Declaring variables gives you somewhere to store values. Operators are how you combine, compare, and test those values — which is really just how you express a business rule in code. "Is stock below reorder level?" is a comparison operator. "Reorder if stock is low AND the product is Electronics" is a logical operator.

Arithmetic Operators

Most of these behave exactly as you'd expect from a calculator, but VBA has two division operators that do genuinely different things:

Dim total As Double
total = 45 * 3      ' multiplication → 135
total = 100 / 3     ' division → 33.33...
total = 100 \ 3     ' integer division → 33 (drops remainder)
total = 100 Mod 3   ' remainder only → 1
total = 2 ^ 3       ' exponent → 8

The backslash (\) and Mod are the two that trip people up, because they look similar but answer opposite questions. 100 \ 3 answers "how many whole times does 3 go into 100?" — the answer is 33, and whatever's left over is thrown away. 100 Mod 3 answers the mirror-image question, "what's left over after 3 goes into 100 as many whole times as it can?" — the answer is 1. Put together, 33 groups of 3 plus a remainder of 1 equals 100 — that's not a coincidence, that's what the two operators are designed to split apart. This pairing is exactly how you'd calculate "how many full boxes of 3 can I pack, and how many units are left over for a partial box?" or check whether a row number is even (i Mod 2 = 0).

Comparison Operators

These return True or False:

44 > 30      ' True
44 = 44      ' True
"Office" = "office"   ' False — VBA's default string comparison IS case-sensitive
44 <> 30     ' True (not equal)

Logical Operators

These combine two or more comparisons into a single True/False result — exactly how a business rule with multiple conditions gets expressed:

If category = "Electronics" And stock < 30 Then
    ' triggers only if BOTH are true
End If
 
If category = "Office" Or category = "Electronics" Then
    ' triggers if EITHER is true
End If
 
If Not (stock < reorderLevel) Then
    ' triggers if stock is NOT below reorder level
End If

And requires every condition joined by it to be True before the whole expression is True — the first example only fires for Electronics products that are also genuinely low on stock, not for every low-stock product regardless of category. Or requires just one condition to be True, which is how you check membership in a small set of acceptable values. Not simply flips a True/False result to its opposite — it's often used, as in the third example, to express a rule as "NOT the bad condition" when that reads more naturally than restating the good condition directly.

String Concatenation

Joins text with & (not +, which VBA reserves for numbers):

Dim message As String
message = "Product: " & productName & " | Stock: " & currentStock
' Product: Wireless Mouse | Stock: 134

Notice currentStock is a number (134), not text, yet it concatenates into the string without any conversion step — VBA automatically converts a number to its text representation when you use & on it. That convenience is exactly why & is preferred over + for joining text: if you wrote "Stock: " + currentStock, VBA would try to interpret the whole expression as arithmetic and either throw an error or produce a confusing result, depending on context. & always means "join as text," with no ambiguity.

Task

  1. Using the Monitor Arm's data (Price 89.99, Stock 18, Reorder Level 10), write an expression that checks whether stock is less than or equal to reorder level plus 10 — a "getting close" warning rather than a hard alert.
  2. Build a concatenated message: "Monitor Arm is at 18 units (reorder at 10)" using variables, not hardcoded text.
Hint
expand arrow

1. The "getting close" expression

  • You need three variables first: something to hold the stock (18), something for the reorder level (10), and then an expression that compares them.
  • "Less than or equal to" is a single comparison operator <=.
  • The phrase "reorder level plus 10" means you're not comparing stock directly against 10 — you're comparing it against a calculated value. Write that addition right inside the comparison: stock <= reorderLevel + 10.
  • To actually see the result, either assign it to a Boolean variable.
  • Work out by hand what 10 + 10 is before you run it, so you know whether 18 <= 20 should come back True or False, and check your code agrees.

2. Building the message

  • The operator that joins text together is &, not +.
  • Look at the target output piece by piece: "Monitor Arm is at " + (the stock number) + " units (reorder at " + (the reorder level) + ")". Count the pieces — there are five, alternating between literal text in quotes and a variable.
  • The task says "using variables, not hardcoded text" — that means the 18 and 10 in your final message should come from your stock and reorderLevel variables, not typed in as literal numbers inside the string.
  • Concatenating a number with & doesn't need any conversion — VBA turns it into text automatically, so you can join a numeric variable directly into the string.
  • Build it into one String variable first (message = ...), then display it with MsgBox message so you can check it matches the expected output exactly, including the parentheses and spacing.

Or yse next code

Open Section_2_VBA_Fundamentals.xlsm, press Alt+F11, and use the module you've been working in for this workbook (insert one via Insert → Module if you don't have one yet — following the same pattern as modIntro in Section 1, you could name it modFundamentals).

Sub MonitorArmCheck()
    Dim stock As Integer
    Dim reorderLevel As Integer
    Dim isGettingClose As Boolean
    Dim message As String

    stock = 18
    reorderLevel = 10

    ' Part 1 — the "getting close" check
    isGettingClose = stock <= reorderLevel + 10
    MsgBox isGettingClose

    ' Part 2 — the concatenated message
    message = "Monitor Arm is at " & stock & " units (reorder at " & reorderLevel & ")"
    MsgBox message
End Sub

Click anywhere inside the Sub, then press F5. You should see two message boxes pop up one after another — first True (since 18 ≤ 20), then the sentence.

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 2.  2

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

Working with Operators

Declaring variables gives you somewhere to store values. Operators are how you combine, compare, and test those values — which is really just how you express a business rule in code. "Is stock below reorder level?" is a comparison operator. "Reorder if stock is low AND the product is Electronics" is a logical operator.

Arithmetic Operators

Most of these behave exactly as you'd expect from a calculator, but VBA has two division operators that do genuinely different things:

Dim total As Double
total = 45 * 3      ' multiplication → 135
total = 100 / 3     ' division → 33.33...
total = 100 \ 3     ' integer division → 33 (drops remainder)
total = 100 Mod 3   ' remainder only → 1
total = 2 ^ 3       ' exponent → 8

The backslash (\) and Mod are the two that trip people up, because they look similar but answer opposite questions. 100 \ 3 answers "how many whole times does 3 go into 100?" — the answer is 33, and whatever's left over is thrown away. 100 Mod 3 answers the mirror-image question, "what's left over after 3 goes into 100 as many whole times as it can?" — the answer is 1. Put together, 33 groups of 3 plus a remainder of 1 equals 100 — that's not a coincidence, that's what the two operators are designed to split apart. This pairing is exactly how you'd calculate "how many full boxes of 3 can I pack, and how many units are left over for a partial box?" or check whether a row number is even (i Mod 2 = 0).

Comparison Operators

These return True or False:

44 > 30      ' True
44 = 44      ' True
"Office" = "office"   ' False — VBA's default string comparison IS case-sensitive
44 <> 30     ' True (not equal)

Logical Operators

These combine two or more comparisons into a single True/False result — exactly how a business rule with multiple conditions gets expressed:

If category = "Electronics" And stock < 30 Then
    ' triggers only if BOTH are true
End If
 
If category = "Office" Or category = "Electronics" Then
    ' triggers if EITHER is true
End If
 
If Not (stock < reorderLevel) Then
    ' triggers if stock is NOT below reorder level
End If

And requires every condition joined by it to be True before the whole expression is True — the first example only fires for Electronics products that are also genuinely low on stock, not for every low-stock product regardless of category. Or requires just one condition to be True, which is how you check membership in a small set of acceptable values. Not simply flips a True/False result to its opposite — it's often used, as in the third example, to express a rule as "NOT the bad condition" when that reads more naturally than restating the good condition directly.

String Concatenation

Joins text with & (not +, which VBA reserves for numbers):

Dim message As String
message = "Product: " & productName & " | Stock: " & currentStock
' Product: Wireless Mouse | Stock: 134

Notice currentStock is a number (134), not text, yet it concatenates into the string without any conversion step — VBA automatically converts a number to its text representation when you use & on it. That convenience is exactly why & is preferred over + for joining text: if you wrote "Stock: " + currentStock, VBA would try to interpret the whole expression as arithmetic and either throw an error or produce a confusing result, depending on context. & always means "join as text," with no ambiguity.

Task

  1. Using the Monitor Arm's data (Price 89.99, Stock 18, Reorder Level 10), write an expression that checks whether stock is less than or equal to reorder level plus 10 — a "getting close" warning rather than a hard alert.
  2. Build a concatenated message: "Monitor Arm is at 18 units (reorder at 10)" using variables, not hardcoded text.
Hint
expand arrow

1. The "getting close" expression

  • You need three variables first: something to hold the stock (18), something for the reorder level (10), and then an expression that compares them.
  • "Less than or equal to" is a single comparison operator <=.
  • The phrase "reorder level plus 10" means you're not comparing stock directly against 10 — you're comparing it against a calculated value. Write that addition right inside the comparison: stock <= reorderLevel + 10.
  • To actually see the result, either assign it to a Boolean variable.
  • Work out by hand what 10 + 10 is before you run it, so you know whether 18 <= 20 should come back True or False, and check your code agrees.

2. Building the message

  • The operator that joins text together is &, not +.
  • Look at the target output piece by piece: "Monitor Arm is at " + (the stock number) + " units (reorder at " + (the reorder level) + ")". Count the pieces — there are five, alternating between literal text in quotes and a variable.
  • The task says "using variables, not hardcoded text" — that means the 18 and 10 in your final message should come from your stock and reorderLevel variables, not typed in as literal numbers inside the string.
  • Concatenating a number with & doesn't need any conversion — VBA turns it into text automatically, so you can join a numeric variable directly into the string.
  • Build it into one String variable first (message = ...), then display it with MsgBox message so you can check it matches the expected output exactly, including the parentheses and spacing.

Or yse next code

Open Section_2_VBA_Fundamentals.xlsm, press Alt+F11, and use the module you've been working in for this workbook (insert one via Insert → Module if you don't have one yet — following the same pattern as modIntro in Section 1, you could name it modFundamentals).

Sub MonitorArmCheck()
    Dim stock As Integer
    Dim reorderLevel As Integer
    Dim isGettingClose As Boolean
    Dim message As String

    stock = 18
    reorderLevel = 10

    ' Part 1 — the "getting close" check
    isGettingClose = stock <= reorderLevel + 10
    MsgBox isGettingClose

    ' Part 2 — the concatenated message
    message = "Monitor Arm is at " & stock & " units (reorder at " & reorderLevel & ")"
    MsgBox message
End Sub

Click anywhere inside the Sub, then press F5. You should see two message boxes pop up one after another — first True (since 18 ≤ 20), then the sentence.

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 2.  2
some-alt