郵便番号は 先頭の数字(または前半3桁)で地域が決まる 仕組みになっています。
これを利用して、郵便番号から都道府県を自動判定する VBA の例を紹介します。
コード例
Sub PostalCodeToPrefecture()
Dim i As Long, lastRow As Long
Dim code As String, prefix As String
' 郵便番号の先頭3桁と都道府県の対応表(簡易版)
Dim prefMap As Object
Set prefMap = CreateObject("Scripting.Dictionary")
' 一部例(実務では全リストを用意)
prefMap.Add "100", "東京都"
prefMap.Add "150", "東京都"
prefMap.Add "220", "神奈川県"
prefMap.Add "530", "大阪府"
prefMap.Add "460", "愛知県"
prefMap.Add "810", "福岡県"
' A列の最終行を取得
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To lastRow
code = Cells(i, 1).Value
' ハイフン削除・空白除去
code = Replace(code, "-", "")
code = Trim(code)
' 郵便番号が7桁なら判定
If Len(code) = 7 And IsNumeric(code) Then
prefix = Left(code, 3) ' 先頭3桁を取得
If prefMap.Exists(prefix) Then
Cells(i, 2).Value = prefMap(prefix) ' B列に都道府県
Else
Cells(i, 2).Value = "不明"
End If
Else
Cells(i, 2).Value = "不正"
End If
Next i
End Sub
VB解説
- 郵便番号の先頭3桁 → 地域を判定するキー
- Dictionary(連想配列) → 「3桁 → 都道府県」の対応表を保持
Left(code, 3)→ 郵便番号の先頭3桁を抽出- 判定結果をB列に出力 → 自動で都道府県が表示される
実行イメージ
| A列(郵便番号) | B列(判定結果) |
|---|---|
| 100-0001 | 東京都 |
| 1500002 | 東京都 |
| 2200012 | 神奈川県 |
| 5300001 | 大阪府 |
| 8100003 | 福岡県 |
| 9999999 | 不明 |
応用ポイント
- 全郵便番号辞書を用意 → 日本郵便の公式データを使えば精度100%に近づく
- 郵便番号から住所自動入力 → 番地以降を補完するシステムに発展可能
- 不正データを別シートに出力 → データ監査に活用できる
💡 この仕組みを使えば「郵便番号から都道府県別に顧客数を集計」や「地域別マーケティング分析」が一気に効率化します。


