'+------------------------------------------------------------------------------
'| 共通変数
'| 参照設定:Microsoft Activex Data Object X.X Library が必要
'+------------------------------------------------------------------------------
Dim JetCn As ADODB.Connection
Dim JetRs As ADODB.Recordset
Public Sub 住所分解()
'================================================================================
' 処理名:住所分解処理(ACCESS版)
' ①住所を、市区名(県名+市区町村名)、字名(大字名+小字名)、番地、方書に分類する
' ②各々の構成要素こどにコード化する(住所マスタ使用)
' Sheets("ADRS_LIST")の設定
' 1,2行目は、見出し、3行目以降がデータ
' A~C列…SEQ№やキー項目
' D列…分解元住所…入力
' E列…郵便番号(XXX-XXXXの形式)、入力あるときは検索しても置換しない
' F列…都道府県コード、G列…市区町村コード、H列…大字コード、I列…小字コード
' J列…番地コード(番地を5桁、5桁、10桁の0埋め数字列にし合体の20桁)
' K列…都道府県名と市区町村名(政令指定都市の県名省略は選択)
' L列…大字小字名(数字部分は漢数字とする…システムによって違う)
' M列…番地(アラビア数字と番地用文字からなる)
' N列…方書(住所補助のアパート、団地、個人名等)
' Sheets("番地使用文字")…A列2行目より番地とみなす文字を列記
' Sheets("都道府県")…A:都道府県コード、B:都道府県名、C,D,E:政令指定都市名
' Sheets("字情報")…例えば自市区町村など頻繁に出現する字名、字コードの一覧、任意
' Sheets("USER")…ACCESS 接続情報(MDB)
'================================================================================
Const Seirei As Boolean = True '政令指定都市都道府県省略フラグ
Dim i As Integer: i = 3 '行
Dim j As Integer '文字位置
Dim k As Integer '列
Dim stAdrs As String '住所ワーク
Dim stSQL As String 'SQL文
Dim stBanti(3) As String '番地コード編集ワーク
Dim OwnAdrs As String '字情報編集ワーク
Sheets("ADRS_LIST").Select '住所展開用シート
Cells(i, 4).Select
Do While Cells(i, 4).Value <> "" '展開対象住所がある間繰り返し
Range(Cells(i, 6), Cells(i, 14)).Value = "" '展開先クリア
'【整形処理】
'①全角化
stAdrs = StrConv(Trim(Cells(i, 4).Value), vbWide)
'②カンマ除去
stAdrs = Replace(stAdrs, ".", "")
'③空白除去
stAdrs = Replace(stAdrs, " ", "")
stAdrs = Replace(stAdrs, " ", "")
'④郵便番号の整形(XXX-XXXX)
If Cells(i, 5).Value <> "" Then
If Len(Cells(i, 5).Value) = 8 And _
Mid(Cells(i, 5).Value, 4, 1) = "-" Then
Cells(i, 5).Value = Replace(Cells(i, 5).Value, " ", "0")
ElseIf Len(Cells(i, 5).Value) = 7 And _
IsNumeric(Cells(i, 5).Value) Then
Cells(i, 5).Value = Left(Cells(i, 5).Value, 3) & "-" & _
Right(Cells(i, 5).Value, 4)
End If
End If
'【市区町村字名と番地を分離】
'①丁目で分割
If InStr(stAdrs, "丁目") > 0 Then
If isANumber(Mid(stAdrs, InStr(stAdrs, "丁目") + 2)) Then
Cells(i, 13).Value = Mid(stAdrs, InStr(stAdrs, "丁目") + 2) '番地格納
stAdrs = 漢数字化(Left(stAdrs, InStr(stAdrs, "丁目") + 1))
Else
Cells(i, 13).Value = Mid(stAdrs, InStr(stAdrs, "丁目") + 3) '番地格納
stAdrs = 漢数字化(Left(stAdrs, InStr(stAdrs, "丁目") + 2))
End If
End If
'②北海道は、条も字名
If Left(stAdrs, 3) = "北海道" Then
j = InStr(stAdrs, "条")
If j > 1 Then
If isNumber(Mid(stAdrs, j - 1)) Then
'条の前後が数字なら条で分割
If isNumber(Mid(stAdrs, j + 1)) Then
Cells(i, 13).Value = Mid(stAdrs, j + 1)
stAdrs = 漢数字化(Left(stAdrs, j))
'条の前と1字後が数字なら1字後で分割
ElseIf isNumber(Mid(stAdrs, j + 2)) Then
Cells(i, 13).Value = Mid(stAdrs, j + 2)
stAdrs = 漢数字化(Left(stAdrs, j + 1))
'条の前と2字後が数字なら2字後で分割
ElseIf isNumber(Mid(stAdrs, j + 3)) Then
Cells(i, 13).Value = Mid(stAdrs, j + 3)
stAdrs = 漢数字化(Left(stAdrs, j + 2))
End If
End If
End If
Cells(i, 6).Value = 1 '北海道
End If
'③大阪府堺市は、丁で分割 (丁ありは丁目なし)
If Left(stAdrs, 5) = "大阪府堺市" Then
j = InStr(stAdrs, "丁")
If j > 1 Then
If isNumber(Mid(stAdrs, j - 1)) Then
'丁の前後が数字なら丁で分割
If isNumber(Mid(stAdrs, j + 1)) Then
Cells(i, 13).Value = Mid(stAdrs, j + 1)
stAdrs = 漢数字化(Left(stAdrs, j))
End If
End If
End If
End If
'④岩手県の「地割」は字名
If Left(stAdrs, 3) = "岩手県" Then
j = InStr(stAdrs, "地割")
If j > 1 Then
stAdrs = 漢数字化(Left(stAdrs, j)) & Mid(stAdrs, j + 1)
End If
End If
'⑤○番町という字名
If InStr(stAdrs, "番町") > 0 Then
j = InStr(stAdrs, "番町")
If isNumber(Mid(stAdrs, j + 2)) Then
Cells(i, 13).Value = Mid(stAdrs, j + 2)
stAdrs = 漢数字化(Left(stAdrs, j + 1))
End If
End If
'⑥アラビア数字で分割
If Cells(i, 13).Value = "" Then '未分割
For j = 1 To Len(stAdrs)
If isANumber(Mid(stAdrs, j, 1)) Then
Cells(i, 13).Value = Mid(stAdrs, j)
stAdrs = Left(stAdrs, j - 1)
Exit For
End If
Next j
End If
'⑦丁目なしの"-"が二つ以上の場合、一つ目の"-"の以前は丁目とみなす
If InStr(stAdrs, "丁") = 0 And _
InStr(Cells(i, 13).Value, "-") > 0 And _
InStr(Cells(i, 13).Value, "-") < InStrRev(Cells(i, 13).Value, "-") Then
If Mid(Cells(i, 13).Value, 2, 1) = "-" Then
stAdrs = stAdrs & 漢数字化(Left(Cells(i, 13).Value, 1)) & "丁目"
Cells(i, 13).Value = Mid(Cells(i, 13).Value, 3)
ElseIf Mid(Cells(i, 13).Value, 3, 1) = "-" Then
stAdrs = stAdrs & 漢数字化(Left(Cells(i, 13).Value, 2)) & "丁目"
Cells(i, 13).Value = Mid(Cells(i, 13).Value, 4)
End If
End If
'【市区名と字名を分離】
'①例外地名
'「郡」がある市
If InStr(stAdrs, "郡上市") > 0 Then
Cells(i, 11).Value = "岐阜県郡上市"
Cells(i, 12).Value = Mid(stAdrs, InStr(stAdrs, "郡上市") + 3)
Cells(i, 6).Value = 21 '岐阜県
Cells(i, 7).Value = 219 '郡上市
ElseIf InStr(stAdrs, "蒲郡市") > 0 Then
Cells(i, 11).Value = "愛知県蒲郡市"
Cells(i, 12).Value = Mid(stAdrs, InStr(stAdrs, "蒲郡市") + 3)
Cells(i, 6).Value = 23 '愛知県
Cells(i, 7).Value = 214 '蒲郡市
ElseIf InStr(stAdrs, "大和郡山市") > 0 Then
Cells(i, 11).Value = "奈良県大和郡山市"
Cells(i, 12).Value = Mid(stAdrs, InStr(stAdrs, "大和郡山市") + 5)
Cells(i, 6).Value = 29 '奈良県
Cells(i, 7).Value = 203 '大和郡山市
'「町」がある町
ElseIf InStr(stAdrs, "杵島郡大町町") > 0 Then
Cells(i, 11).Value = "佐賀県杵島郡大町町"
Cells(i, 12).Value = Mid(stAdrs, InStr(stAdrs, "杵島郡大町町") + 6)
Cells(i, 6).Value = 41 '佐賀県
Cells(i, 7).Value = 423 '杵島郡大町町
'「村」がある町
ElseIf InStr(stAdrs, "柴田郡村田町") > 0 Then
Cells(i, 11).Value = "宮城県柴田郡村田町"
Cells(i, 12).Value = Mid(stAdrs, InStr(stAdrs, "柴田郡村田町") + 6)
Cells(i, 6).Value = 4 '宮城県
Cells(i, 7).Value = 322 '柴田郡村田町
ElseIf InStr(stAdrs, "佐波郡玉村町") > 0 Then
Cells(i, 11).Value = "群馬県佐波郡玉村町"
Cells(i, 12).Value = Mid(stAdrs, InStr(stAdrs, "佐波郡玉村町") + 6)
Cells(i, 6).Value = 10 '群馬県
Cells(i, 7).Value = 464 '柴田郡村田町
'東京都大島町
ElseIf InStr(stAdrs, "東京都大島町") > 0 Then
Cells(i, 11).Value = "東京都大島町"
Cells(i, 12).Value = Mid(stAdrs, 7)
Cells(i, 6).Value = 13 '東京都
Cells(i, 7).Value = 361 '大島町
'東京都八丈町
ElseIf InStr(stAdrs, "東京都八丈町") > 0 Then
Cells(i, 11).Value = "東京都八丈町" '<==東京都八丈島八丈町の場合もあり
Cells(i, 12).Value = Mid(stAdrs, 7)
Cells(i, 6).Value = 13 '東京都
Cells(i, 7).Value = 401 '八丈町
'東京都 & 村
ElseIf InStr(stAdrs, "東京都") > 0 And InStr(stAdrs, "島村") > 0 Then
Cells(i, 11).Value = LTrim(Left(stAdrs, InStr(stAdrs, "村")))
Cells(i, 12).Value = Mid(stAdrs, InStr(stAdrs, "村") + 1)
'東京都小笠原村
ElseIf InStr(stAdrs, "東京都小笠原村") > 0 Then
Cells(i, 11).Value = "東京都小笠原村"
Cells(i, 12).Value = Mid(stAdrs, 8)
Cells(i, 6).Value = 13 '東京都
Cells(i, 7).Value = 421 '小笠原村
'東京都三宅島三宅村
ElseIf InStr(stAdrs, "東京都三宅島三宅村") > 0 Then
Cells(i, 11).Value = "東京都三宅島三宅村"
Cells(i, 12).Value = Mid(stAdrs, 10)
Cells(i, 6).Value = 13 '東京都
Cells(i, 7).Value = 381 '三宅村
'「市」がある村
ElseIf InStr(stAdrs, "余市郡赤井川村") > 0 Then
Cells(i, 11).Value = "北海道余市郡赤井川村"
Cells(i, 12).Value = Mid(stAdrs, InStr(stAdrs, "余市郡赤井川村") + 7)
Cells(i, 6).Value = 1 '北海道
Cells(i, 7).Value = 409 '余市郡赤井川村
ElseIf InStr(stAdrs, "高市郡明日香村") > 0 Then
Cells(i, 11).Value = "奈良県高市郡明日香村"
Cells(i, 12).Value = Mid(stAdrs, InStr(stAdrs, "高市郡明日香村") + 7)
Cells(i, 6).Value = 29 '奈良県
Cells(i, 7).Value = 402 '高市郡明日香村
'「市」がある市
ElseIf InStr(stAdrs, "市川市") > 0 Then
Cells(i, 11).Value = "千葉県市川市"
Cells(i, 12).Value = Mid(stAdrs, InStr(stAdrs, "市川市") + 3)
Cells(i, 6).Value = 12 '千葉県
Cells(i, 7).Value = 203 '市川市
ElseIf InStr(stAdrs, "市原市") > 0 Then
Cells(i, 11).Value = "千葉県市原市"
Cells(i, 12).Value = Mid(stAdrs, InStr(stAdrs, "市原市") + 3)
Cells(i, 6).Value = 12 '千葉県
Cells(i, 7).Value = 219 '市原市
ElseIf InStr(stAdrs, "野々市市") > 0 Then
Cells(i, 11).Value = "石川県野々市市"
Cells(i, 12).Value = Mid(stAdrs, InStr(stAdrs, "野々市市") + 4)
Cells(i, 6).Value = 17 '石川県
Cells(i, 7).Value = 212 '野々市市
ElseIf InStr(stAdrs, "四日市市") > 0 Then
Cells(i, 11).Value = "三重県四日市市"
Cells(i, 12).Value = Mid(stAdrs, InStr(stAdrs, "四日市市") + 4)
Cells(i, 6).Value = 24 '三重県
Cells(i, 7).Value = 202 '四日市市
ElseIf InStr(stAdrs, "廿日市市") > 0 Then
Cells(i, 11).Value = "広島県廿日市市"
Cells(i, 12).Value = Mid(stAdrs, InStr(stAdrs, "廿日市市") + 4)
Cells(i, 6).Value = 34 '広島県
Cells(i, 7).Value = 213 '廿日市市
'②地域自治区(区は字名)
ElseIf InStr(stAdrs, "奥州市") > 0 Then
Cells(i, 11).Value = "岩手県奥州市"
Cells(i, 12).Value = Mid(stAdrs, InStr(stAdrs, "奥州市") + 3)
Cells(i, 6).Value = 3 '岩手県
Cells(i, 7).Value = 215 '奥州市
ElseIf InStr(stAdrs, "南相馬市") > 0 Then
Cells(i, 11).Value = "福島県南相馬市"
Cells(i, 12).Value = Mid(stAdrs, InStr(stAdrs, "南相馬市") + 4)
Cells(i, 6).Value = 7 '福島県
Cells(i, 7).Value = 212 '南相馬市
ElseIf InStr(stAdrs, "宇陀市") > 0 Then
Cells(i, 11).Value = "奈良県宇陀市"
Cells(i, 12).Value = Mid(stAdrs, InStr(stAdrs, "宇陀市") + 3)
Cells(i, 6).Value = 29 '奈良県
Cells(i, 7).Value = 212 '宇陀市
'③町で分割
ElseIf InStr(stAdrs, "郡") > 0 And InStr(stAdrs, "町") > 0 And _
InStr(stAdrs, "郡") < InStr(stAdrs, "町") And _
(InStr(stAdrs, "村") < 1 Or _
InStr(stAdrs, "村") > InStr(stAdrs, "町") Or _
InStr(stAdrs, "村") < InStr(stAdrs, "郡")) Then
Cells(i, 11).Value = LTrim(Left(stAdrs, InStr(stAdrs, "町")))
Cells(i, 12).Value = Mid(stAdrs, InStr(stAdrs, "町") + 1)
'④区で分割 (郡を含む)
ElseIf InStr(stAdrs, "区") > 0 Then
Cells(i, 11).Value = LTrim(Left(stAdrs, InStr(stAdrs, "区")))
Cells(i, 12).Value = Mid(stAdrs, InStr(stAdrs, "区") + 1)
'⑤市で分割
ElseIf InStr(stAdrs, "市") > 0 Then
Cells(i, 11).Value = LTrim(Left(stAdrs, InStr(stAdrs, "市")))
Cells(i, 12).Value = Mid(stAdrs, InStr(stAdrs, "市") + 1)
'⑥村で分割 (郡を含む)
ElseIf InStr(stAdrs, "郡") > 0 And InStr(stAdrs, "村") > 0 And _
InStr(stAdrs, "郡") < InStr(stAdrs, "村") Then
Cells(i, 11).Value = LTrim(Left(stAdrs, InStr(stAdrs, "村")))
Cells(i, 12).Value = Mid(stAdrs, InStr(stAdrs, "村") + 1)
'⑦上記なし…自市区町村の字名で検索し、自市区名を補完
Else
If Sheets("字情報").Cells(1, 2).Value <> "" And _
Sheets("字情報").Cells(2, 2).Value <> "" Then '字情報の設定あり
j = 6
Do While Sheets("字情報").Cells(j, 1).Value <> ""
OwnAdrs = Sheets("字情報").Cells(j, 1).Value & Sheets("字情報").Cells(j, 2).Value
If stAdrs = OwnAdrs Then
If Cells(i, 5).Value = "" Then
Cells(i, 5).Value = Sheets("字情報").Cells(j, 3).Value '郵便番号
End If
Cells(i, 6).Value = Sheets("字情報").Cells(1, 2).Value '都道府県コード
Cells(i, 7).Value = Sheets("字情報").Cells(2, 2).Value '市区町村コード
Cells(i, 8).Value = Sheets("字情報").Cells(j, 4).Value '大字コード
Cells(i, 9).Value = Sheets("字情報").Cells(j, 5).Value '小字コード
Cells(i, 11).Value = Sheets("字情報").Cells(3, 2).Value '市区町村名
Cells(i, 12).Value = OwnAdrs '字名
Exit Do
End If
j = j + 1
Loop
If Cells(i, 11) = "" Then
Cells(i, 12).Value = stAdrs
End If
Else
Cells(i, 11).Value = ""
Cells(i, 12).Value = stAdrs
End If
End If
'【番地と方書の分割】
stAdrs = Cells(i, 13).Value
'①数字と番地用漢字以外がでないのは方書とする
If stAdrs <> "" Then
For j = 1 To Len(stAdrs)
If Mid(stAdrs, j, 2) = "線北" Then '2字の例外
j = j + 1
ElseIf isANumber(Mid(stAdrs, j, 1)) = False And _
isBanti(Mid(stAdrs, j, 1)) = False Then
Cells(i, 13).Value = Left(stAdrs, j - 1)
Cells(i, 14).Value = Mid(stAdrs, j)
Exit For
End If
Next j
End If
'【住所コードの初期化】
If Cells(i, 6).Value = "" Then
Cells(i, 6).Value = -1 '都道府県コード
End If
If Cells(i, 7).Value = "" Then
Cells(i, 7).Value = -1 '市区町村コード
End If
If Cells(i, 8).Value = "" Then
Cells(i, 8).Value = "0000" '大字コード
End If
If Cells(i, 9).Value = "" Then
Cells(i, 9).Value = "0000" '小字コード
End If
If Cells(i, 10).Value = "" Then
Cells(i, 10).Value = "00000000000000000000" '番地コード
End If
'【都道府県コードの設定】
'①県名で県名、及び政令指定都市テーブルテーブルを検索
If Cells(i, 6).Value = -1 Then
For j = 2 To 48
For k = 2 To 5
stAdrs = Sheets("都道府県").Cells(j, k).Value
If stAdrs <> "" And Left(Cells(i, 11).Value, Len(stAdrs)) = stAdrs Then
Cells(i, 6).Value = Sheets("都道府県").Cells(j, 1).Value
Exit For
End If
If stAdrs <> "" And Left(Cells(i, 12).Value, Len(stAdrs)) = stAdrs Then
Cells(i, 6).Value = Sheets("都道府県").Cells(j, 1).Value
Exit For
End If
Next k
If Cells(i, 6).Value <> -1 Then Exit For '既に設定済み
Next j
End If
'【市区町村コードの設定】
'①市区名で住所テーブルを検索 (県コードありなしの2パターン)
' 市区名も県名ありなしの2バターン
Call DB_CONNECT
If Cells(i, 7).Value = -1 Then
stSQL = "SELECT A2.KEYKENCD,A2.KEYSIKUCD,A2.KENMEIKBN,A1.KENMEI,A2.SIKUMEI FROM MST_ADRS1 A1,MST_ADRS2 A2"
stSQL = stSQL & " WHERE A1.KEYKENCD = A2.KEYKENCD AND "
If Cells(i, 6).Value <> -1 Then
stSQL = stSQL & "A1.KEYKENCD = " & Cells(i, 6).Value & " AND "
End If
stSQL = stSQL & "(A2.SIKUMEI = '" & Cells(i, 11).Value
stSQL = stSQL & "' OR A1.KENMEI + A2.SIKUMEI = '" & Cells(i, 11).Value & "')"
Set JetRs = JetCn.Execute(stSQL)
If Not JetRs.EOF Then
Cells(i, 6).Value = JetRs.Fields(0).Value '都道府県コード
Cells(i, 7).Value = JetRs.Fields(1).Value '市町村コード
If Seirei And JetRs.Fields(2).Value = 1 Then
Call Edit_Kenmei(i) '政令指定都市名
ElseIf JetRs.Fields(3).Value <> Left(Cells(i, 11).Value, Len(JetRs.Fields(3).Value)) Then
Cells(i, 11).Value = JetRs.Fields(3).Value & Cells(i, 11).Value
End If
JetRs.Close
Set JetRs = Nothing
End If
End If
'②郡名を除いた市区名で再検索
If Cells(i, 7).Value = -1 And InStr(Cells(i, 11).Value, "郡") > 0 Then
stSQL = "SELECT A2.KEYKENCD,A2.KEYSIKUCD,A2.KENMEIKBN,A1.KENMEI,A2.SIKUMEI FROM MST_ADRS1 A1,MST_ADRS2 A2"
stSQL = stSQL & " WHERE A1.KEYKENCD = A2.KEYKENCD"
If Cells(i, 6).Value <> -1 Then
stSQL = stSQL & " AND A1.KEYKENCD = " & Cells(i, 6).Value
End If
stAdrs = Mid(Cells(i, 11).Value, InStr(Cells(i, 11), "郡") + 1)
stSQL = stSQL & " AND A2.SIKUMEI = '" & stAdrs & "'"
Set JetRs = JetCn.Execute(stSQL)
If Not JetRs.EOF Then
If Cells(i, 6).Value = -1 Then
Cells(i, 6).Value = JetRs.Fields(0).Value '都道府県コード
End If
Cells(i, 7) = JetRs.Fields(1).Value '市町村コード
If Seirei And JetRs.Fields(2).Value = 1 Then
Call Edit_Kenmei(i) '政令指定都市名
ElseIf JetRs.Fields(3).Value <> Left(Cells(i, 11).Value, Len(JetRs.Fields(3).Value)) Then
Cells(i, 11).Value = JetRs.Fields(3).Value & Cells(i, 11).Value
End If
JetRs.Close
Set JetRs = Nothing
End If
End If
' http://blog.goo.ne.jp/frontflug/e/d4d6f95fd0168595cb6ff0fa4b4f1f41 に続く
最新の画像もっと見る
最近の「EXCEL VBA」カテゴリーもっと見る
最近の記事
カテゴリー
- Node.js(14)
- VBScript(2)
- Weblog(314)
- お仕事ツール(0)
- Saloon(1099)
- HTA(32)
- 決め・分け論(57)
- 映画・ドラマ(37)
- EXCEL VBA(35)
- PL/SQL(10)
- Java(11)
- 詩(自作)(5)
- 詩(塚原将)(298)
- 短歌(200)
- 題詠100首鑑賞(96)
- 題詠100首(109)
- ALIAS SMITH and JONES エピソード(1)
- 題詠100首2010(11)
- 読書(73)
- プロスポーツ(プロ野球、格闘技)(27)
- 日常・育児とか(88)
- 仕事(IT関係)(61)
- とほほ(33)
- 夢(32)
- 勝手にバトン(7)
- 写真(36)
- スタートレック視聴日誌草稿(24)
- 旅行(25)
- 嫌いな言葉(6)
- 好きな言葉(4)
バックナンバー
人気記事