色々な様式で入力された電話番号を半角”000-0000-0000″の書式に変換するする関数です。
VBEの標準モジュールに下のコードを登録して「Excelマクロ有効フォーム」で保存してください。
関数の登録方法は⇒♯000 ユーザー定義関数を登録する方法
登録されたら、シート上からユーザー関数「 TelFix」が使用できます。
使い方は
ワークシート上で = TelFix (文字列)の関数として使用できます。
【例】
TelFix (“(03)0000-0000”) → 03-0000-0000
TelFix (“(06)0000番0000号”) → 06-0000-0000
Function TelFix(S)
’全角()付き等の形式で入力させた電話番号を半角”000-0000-0000″の書式に変換する
On Error GoTo EXITFUN
Dim Tel As String
Dim i1 As Long
Dim i2 As Long
Dim LenS As Long
Dim N As String
If S = “” Then
TelFix = “”
Exit Function
End If
Tel = StrConv(S, vbNarrow)
Tel = Replace(Tel, “)”, “-“)
Tel = Replace(Tel, “番”, “-“)
Tel = Replace(Tel, “号”, “”)
LenS = Len(Tel)
For i1 = 1 To LenS
For i2 = 0 To 9
If Mid(Tel, i1, 1) = i2 Then
N = N & i2
End If
Next
If Mid(Tel, i1, 1) = “-” Then
N = N & “-“
End If
Next
If N = “” Then
TelFix = “”
Exit Function
End If
TelFix = N
EXITFUN:
End Function
ここで紹介したコード使用による損害に対しては一切責任は負えません。