☘️

Excelで半角と全角を変換する関数

2024/01/04に公開

カナ以外を半角に変換する

Function z2hexckana(target As String) As String
  Dim charReplace As String, charcur As String
  Dim charAlph As String, charNum As String, charSign As String
  Dim result As String
  Dim i As Long

  charAlph = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  charNum = "0123456789"
  charSign = "!#$%&?()*+?./:;<=>?@[¥]^_{|}。、,,"
  charReplace = charAlph & StrConv(charAlph, vbLowerCase) & charNum & charSign

  result = target
  For i = 1 To Len(charReplace)
    charcur = Mid(charReplace, i, 1)
    result = Replace(result, charcur, StrConv(charcur, vbNarrow))
  Next i

  z2hexckana = result
End Function

半角カナのみを全角に変換する

Function h2zkana(target As String) As String
  Dim charexclude As String, charcur As String
  Dim charAlph As String, charNum As String, charSign As String
  Dim result As String
  Dim i As Long

  result = StrConv(target, vbWide)

  charAlph = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  charNum = "0123456789"
  charSign = "!#$%&?()*+?./:;<=>?@[¥]^_{|}。、,,"
  charexclude = charAlph & StrConv(charAlph, vbLowerCase) & charNum &  charSign

  For i = 1 To Len(charexclude)
    charcur = Mid(charexclude, i, 1)
    result = Replace(result, charcur, StrConv(charcur, vbNarrow))
  Next i

  h2zkana = result
End Function

Discussion