Excel2000 VBA 住所文字列から都道府県名を右のセルに表示 [Excel2000 VBA独習]
Excel2000 VBA 住所文字列から都道府県名を右のセルに表示
選択列(1列)の住所文字列から都道府県名を調べて、右のセルに都道府県名を表示する
実験バージョン
追記
京都府の場合、京、都が含まれるので正しく表示できない(言い訳 普段「京都府」の文字列は扱わないので・・・・)
対策は太字部分
他にもバグがあるかも・・・
Sub 住所文字列から都道府県名を右のセルに表示する()
Dim 住所 As Variant
Dim iti As String Integer '
Dim 都道府県 As String
Dim x As Variant
Dim 検索道府県 As String
Dim j, i As Integer
Dim cell_row_top As Integer
Dim cell_count As Integer
Dim cell_column As Integer
'選択範囲を調べる
cell_column = Selection.Column
cell_row_top = Selection.Row
cell_count = Selection.Rows.Count
For i = cell_row_top To cell_row_top + cell_count - 1
住所 = Cells(i, cell_column).Value
’追記京都府は都府が含まれるので対策が必要
For Each x In Array("京都府","都", "道", "府", "県") 'x は都→道→府→県に変化する
If InStr(住所, x) <> 0 Then 検索道府県 = x : Exit For '住所文字列にx(都)の文字があるか調べる
'該当x(都)文字が有れば検索道府県にx(都)に設定
Next x
iti = InStr(住所, 検索道府県) 'iti Instr()で調べて最初に見つかった文字位置
If 検索道府県 = "京都府" Then iti = 3
都道府県 = Left(住所, iti)
Cells(i, cell_column + 1).Value = 都道府県
Next i
End Sub
コメント 0