見出し画像

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

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

このプロシージャは、Excelで選択したセルに入力されている都道府県に基づいて、その地方名(北海道、東北地方、関東地方など)を自動的に隣の列に表示するものです。

動作の概要

  1. 住所に含まれる都道府県名を確認します。

  2. 正規表現という方法を使い、都道府県名を地方ごとのパターンにマッチさせます。

  3. 該当する都道府県が見つかると、その地方名(例:関東地方、九州地方など)を隣のセルに入力します。

具体的な流れ

  1. 正規表現の設定
    プログラムでは、正規表現(テキストパターンを見つける方法)を使って、都道府県名を検索します。例えば、東京都や神奈川県が含まれる住所があれば、それが「南関東地方」に分類されます。

  2. セルのチェック
    選択したセル(住所が入っているセル)を1つずつ確認し、その中に含まれる都道府県名を正規表現でチェックします。見つかった場合、対応する地方名を隣のセルに入力します。

  3. 隣のセルに地方名を表示
    住所が含まれる地方が見つかった場合、その結果を選択したセルの隣の列に表示します。

ポイント

  • 正規表現を使うことで、複数の都道府県に対して効率よく検索ができます。

  • 該当する地方が見つかった時点で、次のセルの処理に進むため無駄がありません。

例えば、「東京都港区」と入力されているセルがあれば、隣の列に「南関東地方」と表示されます。


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

キーワードハッシュタグ:

#excel #vba #正規表現 #都道府県 #地方区分 #自動入力 #住所検索 #プログラム #エリア判定 #セル操作 #エクセルマクロ #自動化 #効率化 #地方名自動表示 #オフィス活用 #vba初心者 #テキスト処理 #プログラミング #できること #自動処理


英語版

Extract Area Name from Address - Regional Division Version

This explanation is created using ChatGPT.

This procedure automatically displays the regional name (e.g., Hokkaido, Tohoku, Kanto regions) next to the selected cells in Excel, based on the prefecture included in the address.

How it Works

  1. It checks the prefecture name contained in the address.

  2. Using regular expressions, it matches the prefecture name to a predefined pattern for each region.

  3. Once a matching prefecture is found, the corresponding regional name (e.g., Kanto region, Kyushu region) is entered in the adjacent cell.

Detailed Steps

  1. Setting Regular Expressions
    The program uses regular expressions (a method to find text patterns) to search for the prefecture names in the address. For example, if the address contains Tokyo or Kanagawa Prefecture, it gets classified into the "Southern Kanto region."

  2. Checking Each Cell
    The program checks each selected cell (where the address is written) one by one, using the regular expression to search for prefecture names. Once a match is found, it writes the corresponding region name in the adjacent cell.

  3. Displaying the Regional Name in the Adjacent Cell
    When a region is identified based on the address, the result is displayed in the column next to the address.

Key Points

  • By using regular expressions, it efficiently searches for multiple prefecture names.

  • Once a match is found, it moves on to the next cell, optimizing performance.

For instance, if a cell contains "Minato-ku, Tokyo," the next column will display "Southern Kanto region."



Hashtags:

#excel #vba #regex #prefecture #regionaldivision #autoinput #addresssearch #programming #areacheck #celloperation #excelmacro #automation #efficiency #autodisplayregion #officeuse #vbabeginner #textprocessing #coding #productivity #dataprocessing

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