Skip to content

Instantly share code, notes, and snippets.

@lunark
Last active April 7, 2022 02:25
Show Gist options
  • Save lunark/4333441 to your computer and use it in GitHub Desktop.
Save lunark/4333441 to your computer and use it in GitHub Desktop.
VBAマクロ向け関数「CutCityName」市以降の住所をもとに、市町村区名を割り出す(それなりに高速かつ例外処理きちっとしてるはず)FMPro.jpにあるファイルメーカーPro用カスタム関数「City関数」の移植のつもり。 2022/04/07少しだけ改訂。http://www.fmpro.jp/fmi/xsl/plugin/cf.xsl?ID=0019
Public Function CutCityName(ByVal R As String) As String
Dim C As String '市
Dim T As String '町
Dim V As String '村
Dim K As String '区
Dim G As String '郡
If R Like "* *" Then R = Left(R, InStr(1, R, " "))
If R Like "* *" Then R = Left(R, InStr(1, R, " "))
'
Select Case True
'市の例外事項(市名に町、郡、村がある分)
Case R Like "*村山市*"
GoTo CityREIGAI
Case R Like "*原町市*"
GoTo CityREIGAI
Case R Like "*田村市*"
GoTo CityREIGAI
Case R Like "*町田市*"
GoTo CityREIGAI
Case R Like "*東村山市*"
GoTo CityREIGAI
Case R Like "*十日町市*"
GoTo CityREIGAI
Case R Like "*羽村市*"
GoTo CityREIGAI
Case R Like "*武蔵村山市*"
GoTo CityREIGAI
Case R Like "*村上市*"
GoTo CityREIGAI
Case R Like "*中村市*"
GoTo CityREIGAI
Case R Like "*大村市*"
GoTo CityREIGAI
Case R Like "*大町市*"
GoTo CityREIGAI
Case R Like "*郡上市*"
GoTo CityREIGAI
Case R Like "*蒲郡市*"
GoTo CityREIGAI
Case R Like "*大和郡山市*"
GoTo CityREIGAI
Case R Like "*郡山市*"
GoTo CityREIGAI
Case R Like "*小郡市*"
GoTo CityREIGAI
Case R Like "*日向町市*"
GoTo CityREIGAI
Case R Like "*宇城市*" '宇城市内に郡で終わる地名がある 2022/04/07
GoTo CityREIGAI
Case R Like "*八代市*" '八代市内に郡で終わる地名がある 2022/04/07
GoTo CityREIGAI
Case R Like "*日置市*" '日置市内に郡で終わる地名がある 2022/04/07
GoTo CityREIGAI
Case R Like "*南九州市*" '南九州市内に郡で終わる地名がある 2022/04/07
GoTo CityREIGAI
Case R Like "*市*" And R Like "*区*"
' K = Left(R, InStr(1, R, "区"))
' CutCityName = K
'政令指定都市以外は区を付けてはいけない仕組みに変更 2022/04/07
Select Case True
Case R Like "*札幌市*"
K = Left(R, InStr(1, R, "区"))
CutCityName = K
Case R Like "*仙台市*"
K = Left(R, InStr(1, R, "区"))
CutCityName = K
Case R Like "*さいたま市*"
K = Left(R, InStr(1, R, "区"))
CutCityName = K
Case R Like "*千葉市*"
K = Left(R, InStr(1, R, "区"))
CutCityName = K
Case R Like "*横浜市*"
K = Left(R, InStr(1, R, "区"))
CutCityName = K
Case R Like "*川崎市*"
K = Left(R, InStr(1, R, "区"))
CutCityName = K
Case R Like "*相模原市*"
K = Left(R, InStr(1, R, "区"))
CutCityName = K
Case R Like "*新潟市*"
K = Left(R, InStr(1, R, "区"))
CutCityName = K
Case R Like "*静岡市*"
K = Left(R, InStr(1, R, "区"))
CutCityName = K
Case R Like "*浜松市*"
K = Left(R, InStr(1, R, "区"))
CutCityName = K
Case R Like "*名古屋市*"
K = Left(R, InStr(1, R, "区"))
CutCityName = K
Case R Like "*京都市*"
K = Left(R, InStr(1, R, "区"))
CutCityName = K
Case R Like "*大阪市*"
K = Left(R, InStr(1, R, "区"))
CutCityName = K
Case R Like "*堺市*"
K = Left(R, InStr(1, R, "区"))
CutCityName = K
Case R Like "*神戸市*"
K = Left(R, InStr(1, R, "区"))
CutCityName = K
Case R Like "*岡山市*"
K = Left(R, InStr(1, R, "区"))
CutCityName = K
Case R Like "*広島市*"
K = Left(R, InStr(1, R, "区"))
CutCityName = K
Case R Like "北九州市*"
K = Left(R, InStr(1, R, "区"))
CutCityName = K
Case R Like "*福岡市*"
K = Left(R, InStr(1, R, "区"))
CutCityName = K
Case R Like "*熊本市*"
K = Left(R, InStr(1, R, "区"))
CutCityName = K
Case Else
C = Left(R, InStr(1, R, "市"))
CutCityName = C
' Select Case True
' Case R Like "*富山市*"
' CutCityName = "富山市"
End Select
Case R Like "*郡*" And R Like "*町*"
G = Left(R, InStr(1, R, "郡"))
T = Replace(Left(R, InStr(1, R, "町")), G, "")
Select Case True
'郡名に「市」が含まれる町
Case R Like "*余市郡仁木町*" Or R Like "北海道仁木町"
G = "余市郡"
T = "仁木町"
Case R Like "*高市郡高取町*" Or R Like "奈良県高取町"
G = "高市郡"
T = "高取町"
'町名に「市」が入るケース
Case R Like "*余市郡余市町*" Or R Like "北海道余市町*"
G = "余市郡"
T = "余市町"
Case R Like "*九戸郡種市町*" Or R Like "岩手県種市町*"
G = "九戸郡"
T = "種市町"
Case R Like "*芳賀郡市貝町*" Or R Like "栃木県市貝町*"
G = "芳賀郡"
T = "市貝町"
Case R Like "*中新川郡上市町*" Or R Like "富山県上市町*"
G = "中新川郡"
T = "上市町"
Case R Like "*石川郡野々市町*" Or R Like "石川県野々市町*"
G = "石川郡"
T = "野々市町"
Case R Like "*西八代郡市川大門町*" Or R Like "山梨県市川大門町*"
G = "西八代郡"
T = "市川大門町"
Case R Like "*西八代郡市川三郷町*" Or R Like "山梨県市川三郷町*"
G = "西八代郡"
T = "市川三郷町"
Case R Like "*芦品郡新市町*" Or R Like "広島県新市町*"
G = "芦品郡"
T = "新市町"
Case R Like "*香美郡野市町*" Or R Like "高知県野市町*"
G = "香美郡"
T = "野市町"
Case R Like "*日置郡東市来町*" Or R Like "鹿児島県東市来町*"
G = "日置郡"
T = "東市来町"
Case R Like "*日置郡市来町*" Or R Like "鹿児島県市来町*"
G = "日置郡"
T = "市来町"
Case R Like "*吉野郡下市町*" Or R Like "奈良県下市町*"
G = "吉野郡"
T = "下市町"
Case R Like "*神崎郡市川町*" Or R Like "兵庫県市川町*"
G = "神崎郡"
T = "市川町"
Case R Like "*氷上郡市島町*" Or R Like "兵庫県市島町*"
G = "神崎郡"
T = "市島町"
Case R Like "*鹿足郡六日市町*" Or R Like "島根県六日市町*"
G = "鹿足郡"
T = "六日市町"
Case R Like "*阿波郡市場町*" Or R Like "徳島県市場町*"
G = "阿波郡"
T = "市場町"
Case R Like "*西多摩郡五日市町*" Or R Like "東京都五日市町*"
G = "西多摩郡"
T = "五日市町"
Case R Like "*佐伯郡廿日市町*" Or R Like "広島県廿日市町*"
G = "佐伯郡"
T = "廿日市町"
'町名に「村」が含まれるケース
Case R Like "*柴田郡村田町*" Or R Like "宮城県村田町*"
G = "佐伯郡"
T = "村田町"
Case R Like "*佐波郡玉村町*" Or R Like "群馬県玉村町*"
G = "佐波郡"
T = "玉村町"
Case R Like "*中蒲原郡村松町*" Or R Like "新潟県村松町*"
G = "中蒲原郡"
T = "村松町"
Case R Like "*恵那郡岩村町*" Or R Like "岐阜県岩村町*"
G = "恵那郡"
T = "岩村町"
Case R Like "*美方郡村岡町*" Or R Like "兵庫県村岡町*"
G = "美方郡"
T = "村岡町"
Case R Like "*東宇和郡野村町*" Or R Like "愛媛県野村町*"
G = "東宇和郡"
T = "野村町"
'町名に「町」が含まれるケース
Case R Like "*杵島郡大町町*" Or R Like "佐賀県大町町*"
G = "杵島郡"
T = "大町町"
Case R Like "*北松浦郡鹿町町*" Or R Like "長崎県鹿町町*"
G = "北松浦郡"
T = "鹿町町"
'郡名に「村」が含まれる町
Case R Like "*東村山郡*" Or R Like "山形県東村山郡*"
G = "東村山郡"
T = Replace(Left(R, InStr(1, R, "町")), G, "")
Case R Like "*西村山郡*" Or R Like "山形県西村山郡*"
G = "西村山郡"
T = Replace(Left(R, InStr(1, R, "町")), G, "")
Case R Like "*北村山郡*" Or R Like "山形県北村山郡*"
G = "北村山郡"
T = Replace(Left(R, InStr(1, R, "町")), G, "")
Case R Like "*田村郡*" Or R Like "福島県田村郡*"
G = "田村郡"
T = Replace(Left(R, InStr(1, R, "町")), G, "")
'町区名に「町」が含まれる村での誤読対策 2022/04/07
Case R Like "*猿払村*" Or R Like "北海道宗谷郡猿払村*"
G = "宗谷郡"
T = "猿払村" '村だが便宜的に変数「V」を使わない
Case R Like "*平田村*" Or R Like "石川県石川郡平田村*"
G = "石川郡"
T = "平田村" '村だが便宜的に変数「V」を使わない
Case R Like "*川内村*" Or R Like "福島県双葉郡川内村*"
G = "双葉郡"
T = "川内村" '村だが便宜的に変数「V」を使わない
End Select
CutCityName = G & T
Case R Like "*村*" And R Like "*郡*"
G = Left(R, InStr(1, R, "郡"))
V = Replace(Left(R, InStr(1, R, "村")), G, "")
'村名に「市」が含まれるケース
Select Case True
Case R Like "*北津軽郡市浦村*" Or R Like "青森県市浦村*"
G = "北津軽郡"
V = "市浦村"
'郡名に「市」が含まれる村
Case R Like "*余市郡赤井川村*" Or R Like "北海道赤井川村*"
G = "余市郡"
V = "赤井川村"
Case R Like "*高市郡明日香村*" Or R Like "奈良県明日香村*"
G = "高市郡"
V = "明日香村"
'郡名に「村」が含まれる村
Case R Like "*田村郡都路村*" Or R Like "福島県都路村*"
G = "田村郡"
V = "都路村"
End Select
CutCityName = G & V
Case R Like "*郡*"
G = Left(R, InStr(1, R, "郡"))
CutCityName = G
Case R Like "*市*"
CityREIGAI:
C = Left(R, InStr(1, R, "市"))
Select Case True
'市名に「市」が入るケース
Case R Like "*市川市*"
C = "市川市"
Case R Like "*市原市*"
C = "市原市"
Case R Like "*今市市*"
C = "市原市"
Case R Like "*八日市場市*"
C = "八日市場市"
Case R Like "*四日市市*"
C = "四日市市"
Case R Like "*八日市市*"
C = "八日市市"
Case R Like "*廿日市市*"
C = "廿日市市"
Case R Like "*野々市市*"
C = "野々市市"
End Select
CutCityName = C
Case R Like "*区*"
K = Left(R, InStr(1, R, "区"))
CutCityName = K
Case R Like "*村*"
V = Left(R, InStr(1, R, "村"))
CutCityName = V
Case R Like "*町*"
T = Left(R, InStr(1, R, "町"))
Select Case True
'郡名に「市」が含まれる町
Case R Like "*余市郡仁木町*" Or R Like "北海道仁木町"
G = "余市郡"
T = "仁木町"
Case R Like "*高市郡高取町*" Or R Like "奈良県高取町"
G = "高市郡"
T = "高取町"
'町名に「市」が入るケース
Case R Like "*余市郡余市町*" Or R Like "北海道余市町*"
G = "余市郡"
T = "余市町"
Case R Like "*九戸郡種市町*" Or R Like "岩手県種市町*"
G = "九戸郡"
T = "種市町"
Case R Like "*芳賀郡市貝町*" Or R Like "栃木県市貝町*"
G = "芳賀郡"
T = "市貝町"
Case R Like "*中新川郡上市町*" Or R Like "富山県上市町*"
G = "中新川郡"
T = "上市町"
Case R Like "*石川郡野々市町*" Or R Like "石川県野々市町*"
G = "石川郡"
T = "野々市町"
Case R Like "*西八代郡市川大門町*" Or R Like "山梨県市川大門町*"
G = "西八代郡"
T = "市川大門町"
Case R Like "*西八代郡市川三郷町*" Or R Like "山梨県市川三郷町*"
G = "西八代郡"
T = "市川三郷町"
Case R Like "*芦品郡新市町*" Or R Like "広島県新市町*"
G = "芦品郡"
T = "新市町"
Case R Like "*香美郡野市町*" Or R Like "高知県野市町*"
G = "香美郡"
T = "野市町"
Case R Like "*日置郡東市来町*" Or R Like "鹿児島県東市来町*"
G = "日置郡"
T = "東市来町"
Case R Like "*日置郡市来町*" Or R Like "鹿児島県市来町*"
G = "日置郡"
T = "市来町"
Case R Like "*吉野郡下市町*" Or R Like "奈良県下市町*"
G = "吉野郡"
T = "下市町"
Case R Like "*神崎郡市川町*" Or R Like "兵庫県市川町*"
G = "神崎郡"
T = "市川町"
Case R Like "*氷上郡市島町*" Or R Like "兵庫県市島町*"
G = "神崎郡"
T = "市島町"
Case R Like "*鹿足郡六日市町*" Or R Like "島根県六日市町*"
G = "鹿足郡"
T = "六日市町"
Case R Like "*阿波郡市場町*" Or R Like "徳島県市場町*"
G = "阿波郡"
T = "市場町"
Case R Like "*西多摩郡五日市町*" Or R Like "東京都五日市町*"
G = "西多摩郡"
T = "五日市町"
Case R Like "*佐伯郡廿日市町*" Or R Like "広島県廿日市町*"
G = "佐伯郡"
T = "廿日市町"
'町名に「村」が含まれるケース
Case R Like "*柴田郡村田町*" Or R Like "宮城県村田町*"
G = "佐伯郡"
T = "村田町"
Case R Like "*佐波郡玉村町*" Or R Like "群馬県玉村町*"
G = "佐波郡"
T = "玉村町"
Case R Like "*中蒲原郡村松町*" Or R Like "新潟県村松町*"
G = "中蒲原郡"
T = "村松町"
Case R Like "*恵那郡岩村町*" Or R Like "岐阜県岩村町*"
G = "恵那郡"
T = "岩村町"
Case R Like "*美方郡村岡町*" Or R Like "兵庫県村岡町*"
G = "美方郡"
T = "村岡町"
Case R Like "*東宇和郡野村町*" Or R Like "愛媛県野村町*"
G = "東宇和郡"
T = "野村町"
'町名に「町」が含まれるケース
Case R Like "*杵島郡大町町*" Or R Like "佐賀県大町町*"
G = "杵島郡"
T = "大町町"
Case R Like "*北松浦郡鹿町町*" Or R Like "長崎県鹿町町*"
G = "北松浦郡"
T = "鹿町町"
Case R Like "*富良野町*"
T = "富良野町"
End Select
CutCityName = T
End Select
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment