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

Len関数の使い方と注意点

Len関数は、文字列の長さ(文字数)を取得する関数です。
全角・半角・記号・日本語を問わず、文字単位でカウントされます。

構文

Len( 文字列 )
  • 文字列:文字数を取得したい文字列やセルの値、変数などを指定します。

使用例

以下は、さまざまな文字種を対象に Len 関数を使った例です。

Sub Sample()
    Dim mystring1 As String: mystring1 = "Abc"
    Debug.Print "半角英字 : " & Len(mystring1)

    Dim mystring2 As String: mystring2 = "Abc"
    Debug.Print "全角英字 : " & Len(mystring2)

    Dim mystring3 As String: mystring3 = "あいう"
    Debug.Print "ひらがな : " & Len(mystring3)

    Dim mystring4 As String: mystring4 = "亜伊宇"
    Debug.Print "漢字 : " & Len(mystring4)

    Dim mystring5 As String: mystring5 = "!?#"
    Debug.Print "半角記号 : " & Len(mystring5)

    Dim mystring6 As String: mystring6 = "!?#"
    Debug.Print "全角記号 : " & Len(mystring6)

    Dim mystring7 As String: mystring7 = "123"
    Debug.Print "半角数字 : " & Len(mystring7)
    
    Dim mystring8 As String: mystring8 = "123"
    Debug.Print "全角数字 : " & Len(mystring8)

    Dim mystring9 As String: mystring9 = "A b C"    ' 半角スペース+全角スペース
    Debug.Print "スペース : " & Len(mystring9)
End Sub

▶ 出力結果

半角英字 : 3
全角英字 : 3
ひらがな : 3
漢字 : 3
半角記号 : 3
全角記号 : 3
半角数字 : 3
全角数字 : 3
スペース : 5

全角や半角、英字やひらがなに関係なく文字数を出力しています。
また、文字列にスペースを含む場合はスペースの数も含めて算出されています。

⚠️注意

引数に数値型や他の型に使用すると、文字数ではなく、格納に必要なバイト数を返します。

Sub Sample()
    Dim MyInteger1 As Integer
    MyInteger1 = 123
    Debug.Print "Integer1 : " & Len(MyInteger1)
    
    Dim MyInteger2 As Integer
    MyInteger2 = -12345
    Debug.Print "Integer2 : " & Len(MyInteger2)
    
    Dim MyLong As Long
    MyLong = 12345678
    Debug.Print "Long : " & Len(MyLong)
    
    Dim MyDouble As Double
    MyDouble = 3.14
    Debug.Print "Double : " & Len(MyDouble)
    
    Dim MyDate As Date
    MyDate = 2025 / 4 / 1
    Debug.Print "Date : " & Len(MyDate)
    
    Dim MyBoolean As Boolean
    MyBoolean = True
    Debug.Print "Boolean : " & Len(MyBoolean)

    Dim MyVariant1 As Variant
    MyVariant1 = #4/1/2025#
    Debug.Print "Variant1 : " & Len(MyVariant1)

    Dim MyVariant2 As Variant
    MyVariant2 = True
    Debug.Print "Variant2 : " & Len(MyVariant2)
End Sub

▶ 出力結果

Integer1 : 2
Integer2 : 2
Long : 4
Double : 8
Date : 8
Boolean : 2
Variant1 : 10   
Variant2 : 4    

※Variant型は内容によって型が変わるため値が変化します。
 #4/1/2025# → Date型の8バイト+Variant型として格納するための2バイト → 10
 #True → Boolean型 → Boolean型の2バイト+Variant型として格納するための2バイト → 4


正確に調べたい場合は、CStr関数で文字列に変換してから

数値を文字列に変換してから Len で文字数を取得すると、意図通りの結果が得られます。

Sub Sample()
    Dim myint As Integer
    myint = 12345
    Debug.Print Len(Cstr(myint))
End Sub

▶ 出力結果

5

その他のVBA関数

【Excel VBA】VBAでよく使う関数一覧&基本の使い方

参考リンク

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