1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

知らないと怖いExcelVBAの穴15~Booleanの仕様~

Last updated at Posted at 2026-01-09

1. はじめに

多くのプログラミング言語では、

  • True1
  • False0

となっています。

Python や JavaScript でも、
真偽値を数値として扱う場合は基本的に 1 / 0 になります。
そもそも数値として扱わせない言語もあります。

そのため、 VBAでも同じだろうと無意識に思ってしまいがちです。

ここに、Excel VBA 特有の落とし穴があります。

2. VBAの Boolean は 1 / 0 ではない

VBAでは、Boolean 型は次のように扱われます。

  • True-1
  • False0
VBA
Debug.Print True    ' -1
Debug.Print False   ' 0

つまり VBA では、 True は 1 ではない

この違いは、
条件分岐だけを書いているうちは問題になりません。

VBA
If flg Then
    ' 処理
End If

しかし、Boolean を 数値として扱おうとすると失敗します

VBA
Dim flg As Boolean
flg = True

Debug.Print flg + 1   ' 結果は 0

「True = 1」だと思っていると、完全に予想外の結果になります。

3. まとめ

  • 他の言語の多くは True / False =  1 / 0
  • VBAでは -1 / 0
  • Boolean は数値として扱わない

このように VBAくらいでしか見られない仕様が意外と多いです。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?