見出し画像

住所からエリア名を隣列にだすよ八地方区分版

この説明は、ChatGPTで作成しています。

このプロシージャは、住所 が入力されているセルを選択して、その住所に該当するエリア名を隣の列に自動で表示するプログラムです。日本全国の都道府県を8つの地方区分に分けて、それに基づいてエリア名を表示します。プログラムの動作を順番に説明していきますね。


1. 住所 を分析してエリア名を探す準備

まず、正規表現 を使って、住所から特定のエリア(例えば「北海道」や「東京都」など)を探し出すためのパターンを用意しています。正規表現とは、テキストの中から特定の文字の並びを探すための方法です。

  • `areaPatterns` に、各地方に属する都道府県の名前が入っています。例えば、「(東京都|神奈川県|埼玉県|…)」というふうにまとめられています。

  • `areaNames` には、エリアに対応する名前(北海道地方、関東地方など)が入っています。


2. 住所をチェックしてエリアを判定

選択されたセル(住所が入っているところ)を一つずつ確認していきます。プログラムは、まず住所がどの地方に属するかを正規表現で確認し、一致したら、そのエリア名をそのセルの隣の列に表示します。

具体的な流れは以下の通りです。

  1. 住所が書かれているセル(選択範囲の各セル)を順番に処理します。

  2. 各セルの住所と、事前に用意された エリアの正規表現パターン を比較します。

  3. 一致したら、そのエリア名(例えば「関東地方」など)を、隣のセルに表示します。

  4. 一度エリアが見つかったら、次の住所のチェックに進みます。


3. 実際に使うとどうなる?

例えば、あるセルに「東京都港区」と書いてあれば、このプログラムを実行すると、その隣のセルに「関東地方」と表示されます。こうして、複数の住所に対して、一気にエリア名を出すことができます。


このプログラムは、仕事で全国の住所データを扱う時に、エリアごとに分類 したい場合に便利です。Excelの選択範囲に対して簡単に操作できるため、初心者でも使いやすいですよ。


Sub 住所からエリア名を隣列にだすよ八地方区分版()
    Dim regEx As Object
    Set regEx = CreateObject("VBScript.RegExp")
    Dim myRng As Range
    Dim areaPatterns As Variant
    Dim areaNames As Variant
    Dim i As Integer
    
    ' エリアごとの正規表現パターン
    areaPatterns = Array( _
        "(北海道)", _
        "(青森県|岩手県|宮城県|秋田県|山形県|福島県)", _
        "(茨城県|栃木県|群馬県|埼玉県|千葉県|東京都|神奈川県)", _
        "(新潟県|富山県|石川県|福井県|山梨県|長野県|岐阜県|静岡県|愛知県)", _
        "(三重県|滋賀県|京都府|大阪府|兵庫県|奈良県|和歌山県)", _
        "(鳥取県|島根県|岡山県|広島県|山口県)", _
        "(徳島県|香川県|愛媛県|高知県)", _
        "(福岡県|佐賀県|長崎県|熊本県|大分県|宮崎県|鹿児島県|沖縄県)" _
    )
    
    ' エリア名
    areaNames = Array( _
        "北海道地方", _
        "東北地方", _
        "関東地方", _
        "中部地方", _
        "近畿地方", _
        "中国地方", _
        "四国地方", _
        "九州・沖縄地方" _
    )
    
    ' 各セルをチェック
    For Each myRng In Selection
        For i = LBound(areaPatterns) To UBound(areaPatterns)
            Set regEx = CreateObject("VBScript.RegExp")
            regEx.Pattern = areaPatterns(i)
            regEx.Global = True
            If regEx.Test(myRng.Value) Then
                myRng.Offset(0, 1).Value = areaNames(i)
                Exit For ' 一度エリアが見つかったら、次のセルに進む
            End If
        Next i
    Next myRng
End Sub

関連キーワード

#vba #excel #住所検索 #エリア名抽出 #日本地図 #地方区分 #エリア分類 #プログラミング初心者 #関数 #正規表現 #VBA活用 #仕事効率化 #自動化 #住所解析 #Excel機能 #エクセル自動化 #マクロ作成 #関東地方 #全国対応 #できること



Translation (English Version)

Extracting Area Name from Address - Eight Regional Divisions Version

This explanation is created with ChatGPT.

This procedure automatically displays the corresponding area name next to the column containing addresses. The program divides Japan's prefectures into 8 regions and uses that to identify the area name based on the address. Let’s go through the functionality step by step.


1. Preparing to Analyze the Address and Identify the Area Name

First, the program uses regular expressions to find specific areas (e.g., "Hokkaido" or "Tokyo") from the given address. Regular expressions are a method used to search for specific text patterns.

  • `areaPatterns` contains patterns for prefectures in each region. For instance, "(Tokyo|Kanagawa|Saitama|…)" is grouped together for the Kanto area.

  • `areaNames` contains the corresponding area names, such as "Hokkaido Region" or "Kanto Region."


2. Checking the Address and Identifying the Area

The program goes through each selected cell (where the addresses are written) and checks them one by one. It matches the address with the pre-defined area patterns using regular expressions. If a match is found, the corresponding area name is displayed in the cell next to it.

Here’s the process in detail:

  1. The program processes each selected cell (containing addresses) in order.

  2. It compares the address in each cell with the predefined regular expression patterns for the areas.

  3. Once a match is found, the area name (e.g., "Kanto Region") is displayed in the cell next to it.

  4. After finding the area for one address, the program moves on to check the next one.


3. How Does It Work in Practice?

For instance, if a cell contains "Minato-ku, Tokyo," running this program will display "Kanto Region" in the next column. This way, the area names for multiple addresses can be extracted all at once.


This program is handy when dealing with address data from all over Japan and classifying them by region. Since it works on a selected range in Excel, it's easy to use even for beginners.



Related Keywords

#vba #excel #addresslookup #areanameextraction #japanmap #regionaldivision #areaclassification #programmingforbeginners #functions #regexpattern #vbauseful #workflowefficiency #automation #addressanalysis #exceltools #excelautomation #macrocreation #kantoregion #nationwidecoverage #whatexcelcando

この記事が気に入ったらサポートをしてみませんか?