![](https://excel-vba.work/wp-content/themes/cocoon-master/images/b-woman.png)
VBAで
・文字数をチェック
できます!
![](https://excel-vba.work/wp-content/themes/cocoon-master/images/b-woman.png)
VBA関数である
・Len関数
によりチェックできます!
VBAコード
ここでは例として、
・シート「sample」のセル「B3」の値を取得して
・値の文字数が5桁以上がどうかをチェック
します。
![セル「B3」の値。5桁(=5桁以上)が設定されている。](https://excel-vba.work/wp-content/uploads/2022/05/vba_len_count_check_001.png)
Option Explicit
Sub sample()
Dim targetStr As String
'判定したいデータを取得
targetStr = Worksheets("sample").range("B3")
'文字数が5桁以上がどうかをチェック
If Len(targetStr) <= 5 Then
MsgBox "5桁以上が入力されています。5桁未満を入力してください。"
End If
End Sub
実行結果
文字数をチェックできました。
![実行結果](https://excel-vba.work/wp-content/uploads/2022/05/vba_len_count_check_002.png)
参考
上記のVBAコードで使用した以下の詳細は、公式サイトをご確認ください。
●VBA関数「Len」