LoginSignup
0
1

More than 5 years have passed since last update.

【VBA】文字列がすべて全角か判定する

Posted at

【小ネタです】
ユーザーフォームやセル上に入力された文字列が
すべて全角かどうか判定したいときがある。
(例:名前は全角のみ、など)

こんな時、下記の関数を作成し実行すれば判定可能

Public Function IsAllCharWide(ByVal Strings As String) As Boolean
'---------------------------------------
'--- 引数:Strings -> 判定する文字列
'---  返値:True -> Stringsはすべて全角
'---       False -> Stringsに半角が含まれる
'---------------------------------------

Dim i As Long
Dim Length As Long
Dim strANSI As String

Length = Len(Strings)  
    For i = 1 To Length
        strANSI = StrConv(Mid(Strings, i, 1), vbFromUnicode) '---UnicodeからANSIに変換。1文字ずつチェックする。---
        '---バイト数を計算---
            Select Case LenB(strANSI)
                Case Is = 1
                    IsAllCharWide = False
                    Exit Function
            End Select
        '------
    Next i
    IsAllCharWide = True
End Function

動作確認済み環境

  • Excel
    • 2007,2010
  • Access
    • 2003,2007,2010,2013
0
1
2

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
1