Excelのセルを選択した範囲について英字や数字の全角と半角を相互に変換するマクロを作りました。
Sub 選択範囲の全角半角を変換()
Dim c As Object
res = InputBox("1:英字を全角から半角 2:英字を半角から全角 3:数字を全角から半角 4:数字を半角から全角", "全角半角変換", "1")
If res = "1" Then
For Each c In Selection
c.Value = alphabet_zen2han(c.Value)
Next c
ElseIf res = "2" Then
For Each c In Selection
c.Value = alphabet_han2zen(c.Value)
Next c
ElseIf res = "3" Then
For Each c In Selection
c.Value = number_zen2han(c.Value)
Next c
ElseIf res = "4" Then
For Each c In Selection
c.Value = number_han2zen(c.Value)
Next c
End If
End Sub
Function alphabet_zen2han(moto)
For i = 1 To Len(moto)
If Mid(moto, i, 1) Like "[A-Za-z]" Then
Mid(moto, i, 1) = StrConv(Mid(moto, i, 1), vbNarrow)
End If
Next i
alphabet_zen2han = moto
End Function
Function alphabet_han2zen(moto)
For i = 1 To Len(moto)
If Mid(moto, i, 1) Like "[A-Za-z]" Then
Mid(moto, i, 1) = StrConv(Mid(moto, i, 1), vbWide)
End If
Next i
alphabet_han2zen = moto
End Function
Function number_zen2han(moto)
For i = 1 To Len(moto)
If Mid(moto, i, 1) Like "[0-9]" Then
Mid(moto, i, 1) = StrConv(Mid(moto, i, 1), vbNarrow)
End If
Next i
number_zen2han = moto
End Function
Function number_han2zen(moto)
For i = 1 To Len(moto)
If Mid(moto, i, 1) Like "[0-9]" Then
Mid(moto, i, 1) = StrConv(Mid(moto, i, 1), vbWide)
End If
Next i
number_han2zen = moto
End Function
Dim c As Object
res = InputBox("1:英字を全角から半角 2:英字を半角から全角 3:数字を全角から半角 4:数字を半角から全角", "全角半角変換", "1")
If res = "1" Then
For Each c In Selection
c.Value = alphabet_zen2han(c.Value)
Next c
ElseIf res = "2" Then
For Each c In Selection
c.Value = alphabet_han2zen(c.Value)
Next c
ElseIf res = "3" Then
For Each c In Selection
c.Value = number_zen2han(c.Value)
Next c
ElseIf res = "4" Then
For Each c In Selection
c.Value = number_han2zen(c.Value)
Next c
End If
End Sub
Function alphabet_zen2han(moto)
For i = 1 To Len(moto)
If Mid(moto, i, 1) Like "[A-Za-z]" Then
Mid(moto, i, 1) = StrConv(Mid(moto, i, 1), vbNarrow)
End If
Next i
alphabet_zen2han = moto
End Function
Function alphabet_han2zen(moto)
For i = 1 To Len(moto)
If Mid(moto, i, 1) Like "[A-Za-z]" Then
Mid(moto, i, 1) = StrConv(Mid(moto, i, 1), vbWide)
End If
Next i
alphabet_han2zen = moto
End Function
Function number_zen2han(moto)
For i = 1 To Len(moto)
If Mid(moto, i, 1) Like "[0-9]" Then
Mid(moto, i, 1) = StrConv(Mid(moto, i, 1), vbNarrow)
End If
Next i
number_zen2han = moto
End Function
Function number_han2zen(moto)
For i = 1 To Len(moto)
If Mid(moto, i, 1) Like "[0-9]" Then
Mid(moto, i, 1) = StrConv(Mid(moto, i, 1), vbWide)
End If
Next i
number_han2zen = moto
End Function
コメント