英字、数値、スペースのみ半角に変換する

Function fncConvertAlphanumericAndSpaceToHalfWidth(strInput As String) As String
    Dim i As Long
    Dim strOutput As String: strOutput = ""
    Dim char As String
    Dim charCode As Long

    For i = 1 To Len(strInput)
        char = Mid(strInput, i, 1)
        charCode = AscW(char)

        ' 全角英数字を半角に変換
        If (charCode >= &HFF10 And charCode <= &HFF19) Or (charCode >= &HFF21 And charCode <= &HFF3A) Or (charCode >= &HFF41 And charCode <= &HFF5A) Then
            strOutput = strOutput & ChrW(charCode - &HFEE0)
        ' 全角スペースを半角に変換
        ElseIf charCode = &H3000 Then
            strOutput = strOutput & Chr(32)
        Else
            strOutput = strOutput & char
        End If
    Next i

    fncConvertAlphanumericAndSpaceToHalfWidth = strOutput
End Function

コメント