3
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?

VBA 記号の大小関係

Last updated at Posted at 2025-12-08

これは「VBA Advent Calendar 2025」の10日目です
https://adventar.org/calendars/11327

"記号" と "0"を大小比較してみる

Debug.Print (" " > "0")  'False
Debug.Print ("!" > "0")  'False
Debug.Print ("#" > "0")  'False
Debug.Print ("$" > "0")  'False
Debug.Print ("%" > "0")  'False
Debug.Print ("&" > "0")  'False
Debug.Print ("(" > "0")  'False
Debug.Print (")" > "0")  'False
Debug.Print ("+" > "0")  'False
Debug.Print ("*" > "0")  'False
Debug.Print ("-" > "0")  'False
Debug.Print ("/" > "0")  'False
Debug.Print ("," > "0")  'False
Debug.Print ("." > "0")  'False
Debug.Print ("'" > "0")  'False
Debug.Print ("""" > "0") 'False

Debug.Print ("=" > "0")  'True
Debug.Print ("~" > "0")  'True
Debug.Print ("|" > "0")  'True
Debug.Print ("`" > "0")  'True
Debug.Print ("{" > "0")  'True
Debug.Print ("}" > "0")  'True
Debug.Print ("<" > "0")  'True
Debug.Print (">" > "0")  'True
Debug.Print ("[" > "0")  'True
Debug.Print ("]" > "0")  'True
Debug.Print ("?" > "0")  'True
Debug.Print ("_" > "0")  'True
Debug.Print ("^" > "0")  'True
Debug.Print ("\" > "0")  'True
Debug.Print ("@" > "0")  'True
Debug.Print (";" > "0")  'True
Debug.Print (":" > "0")  'True

⇒ "0"より小さい記号と"0"より大きい記号が存在することが分かる。

"記号" と "0"を等価確認してみる

念のため"0"であるか確認する。

Debug.Print (" " = "0")  'False
Debug.Print ("!" = "0")  'False
Debug.Print ("#" = "0")  'False
Debug.Print ("$" = "0")  'False
Debug.Print ("%" = "0")  'False
Debug.Print ("&" = "0")  'False
Debug.Print ("(" = "0")  'False
Debug.Print (")" = "0")  'False
Debug.Print ("+" = "0")  'False
Debug.Print ("*" = "0")  'False
Debug.Print ("-" = "0")  'False
Debug.Print ("/" = "0")  'False
Debug.Print ("," = "0")  'False
Debug.Print ("." = "0")  'False
Debug.Print ("'" = "0")  'False
Debug.Print ("""" = "0") 'False

Debug.Print ("=" = "0")  'False
Debug.Print ("~" = "0")  'False
Debug.Print ("|" = "0")  'False
Debug.Print ("`" = "0")  'False
Debug.Print ("{" = "0")  'False
Debug.Print ("}" = "0")  'False
Debug.Print ("<" = "0")  'False
Debug.Print (">" = "0")  'False
Debug.Print ("[" = "0")  'False
Debug.Print ("]" = "0")  'False
Debug.Print ("?" = "0")  'False
Debug.Print ("_" = "0")  'False
Debug.Print ("^" = "0")  'False
Debug.Print ("\" = "0")  'False
Debug.Print ("@" = "0")  'False
Debug.Print (";" = "0")  'False
Debug.Print (":" = "0")  'False

⇒ すべてFalseである。この中に"0"と等価の記号はない。

そもそもTrueになるのは、同一文字のみのはず。

Debug.Print ("@" = "@")  'Ture

"@" と "数値"を大小比較してみる

実験台として"@"には犠牲になってもらう。
先ほどの検証によると、"@"は"0"より大きいらしい。

'Long型の最大値
Debug.Print ("@" > "2147483647")  'Ture

'LongLong型の最大値
Debug.Print ("@" > "9223372036854775807")  'Ture

'Decimal型の最大値
Debug.Print ("@" > "79228162514264337593543950335")  'Ture
    
'1 googol
Debug.Print ("@" > "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")  'Ture

⇒ "@"は1 googolより大きい。

"@"を数値と比較するのはナンセンスだと分かった。

"@" と "記号"を大小比較してみる

Debug.Print (" " > "@")  'False
Debug.Print ("!" > "@")  'False
Debug.Print ("#" > "@")  'False
Debug.Print ("$" > "@")  'False
Debug.Print ("%" > "@")  'False
Debug.Print ("&" > "@")  'False
Debug.Print ("(" > "@")  'False
Debug.Print (")" > "@")  'False
Debug.Print ("+" > "@")  'False
Debug.Print ("*" > "@")  'False
Debug.Print ("-" > "@")  'False
Debug.Print ("/" > "@")  'False
Debug.Print ("," > "@")  'False
Debug.Print ("." > "@")  'False
Debug.Print ("'" > "@")  'False
Debug.Print ("""" > "@") 'False

Debug.Print ("=" > "@")  'False
Debug.Print ("~" > "@")  'True
Debug.Print ("|" > "@")  'True
Debug.Print ("`" > "@")  'True
Debug.Print ("{" > "@")  'True
Debug.Print ("}" > "@")  'True
Debug.Print ("<" > "@")  'False
Debug.Print (">" > "@")  'False
Debug.Print ("[" > "@")  'True
Debug.Print ("]" > "@")  'True
Debug.Print ("?" > "@")  'False
Debug.Print ("_" > "@")  'True
Debug.Print ("^" > "@")  'True
Debug.Print ("\" > "@")  'True
Debug.Print ("@" > "@")  'False
Debug.Print (";" > "@")  'False
Debug.Print (":" > "@")  'False

⇒ "@"より小さい記号と大きい記号が存在する。

記号同士を大小比較してみる

"記号"と"数字"を小さい順に並び替えてみることにした。

Sub exe()
    '記号と数字を配列に格納
    Dim ary() As Variant
    ary = Array(" ", "!", "#", "$", "%", "&", "(", ")", "+", "*", "-", "/", ",", ".", "'", """", "=", "~", "|", "`", "{", "}", "<", ">", "[", "]", "?", "_", "^", "\", "@", ";", ":", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
    
    '小さい順にソート(ソートのコードは割愛)
    ary = quickSortAry(ary, LBound(ary), UBound(ary))
    
    '配列の中身を出力
    Dim val As Variant
    For Each val In ary
        Debug.Print val & "/" & AscW(val) '文字、Unicodeコードポイントを出力
    Next val
End Sub

出力結果

 /32
!/33
"/34
#/35
$/36
%/37
&/38
'/39
(/40
)/41
*/42
+/43
,/44
-/45
./46
//47
0/48
1/49
2/50
3/51
4/52
5/53
6/54
7/55
8/56
9/57
:/58
;/59
</60
=/61
>/62
?/63
@/64
[/91
\/92
]/93
^/94
_/95
`/96
{/123
|/124
}/125
~/126

なんとなく予想はついていたけど、記号の大小関係≣Unicodeコードポイント順だった。

ちなみにExcelで昇順で並べると、異なる並び順になる。
概ねUnicodeコードポイント順に並んでいるが、いくつかは規則性から外れている。(黄色セル)

image.png

なんの順なんだろう…?
分からないけど、今回はこれでおしまい。

追記

Debug.Print (9 < 100)       'True
Debug.Print ("9" < "100")   'False

「"9" < "100"」がFalseになっている。
これは恐らく「9 < 100」ではなく「"9" < "1"」であり、
Unicodeコードポイントで言うと「57 < 49」であると思われる。

3
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
3
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?