見出し画像

カッコのなかにある駅名を伏字にするよ

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

プロシージャの概要

このVBAプロシージャは、選択されたセル範囲内のテキストに含まれる駅名を伏字(「●●駅」)に置き換えるものです。例えば、「東京駅」や「大阪駅」が「●●駅」になります。

詳細な説明

  1. 画面更新の停止

    • Application.ScreenUpdating = False により、処理中に画面の更新が止まり、パフォーマンスが向上します。

  2. 正規表現オブジェクトの作成

    • CreateObject("VBScript.RegExp") を使って、正規表現オブジェクトを作成します。このオブジェクトを使用して、特定のパターンをテキスト内で検索します。

  3. 正規表現のパターン設定

    • With reg ブロック内で、駅名を検索するためのパターンを設定します。ここでは、*「\W.駅」 というパターンを使用しています。

    • \W は非単語文字(例えば、スペースや句読点)を意味し、. は任意の文字列、駅 は駅という文字列です。これにより、「(任意の文字列)駅」という形式の駅名が検索されます。

  4. 選択範囲内のセルを処理

    • For Each myRng In Selection により、選択されたセル範囲内の各セルを順番に処理します。

    • txt = myRng.Value でセルの値を取得し、reg.Replace(txt, "「●●駅」") によって、パターンに一致する駅名を「●●駅」に置き換えます。

    • 置き換えた結果を再びセルに戻します。

  5. エラー処理

    • On Error Resume Next により、エラーが発生しても処理を続行します。

  6. 画面更新の再開

    • Application.ScreenUpdating = True で、画面の更新を再開します。

このプロシージャを実行すると、選択されたセル範囲内のテキストの中にある駅名が伏字になります。

Excel VBA リファレンス | Microsoft Learn
この記事のYouTube動画はこちら


Anonymize Station Names in Parentheses

This explanation is created by ChatGPT.

Overview of the Procedure

This VBA procedure replaces station names within parentheses in the selected cell range with a placeholder ("●●駅"). For example, "Tokyo Station" or "Osaka Station" becomes "●●駅".

Detailed Explanation

  1. Stopping Screen Updating

    • Application.ScreenUpdating = False stops the screen from updating during the process, which improves performance.

  2. Creating Regular Expression Object

    • Using CreateObject("VBScript.RegExp"), a regular expression object is created. This object is used to search for specific patterns within the text.

  3. Setting Regular Expression Pattern

    • Within the With reg block, a pattern is set to search for station names. The pattern *「\W.駅」 is used here.

    • \W signifies a non-word character (e.g., space or punctuation), . signifies any string, and 駅 represents the character for "station". This pattern searches for "(any string)station".

  4. Processing Each Cell in the Selected Range

    • For Each myRng In Selection processes each cell within the selected range sequentially.

    • txt = myRng.Value gets the value of the cell, and reg.Replace(txt, "「●●駅」") replaces the matched station names with "●●駅".

    • The replaced result is then set back to the cell.

  5. Error Handling

    • On Error Resume Next allows the process to continue even if an error occurs.

  6. Resuming Screen Updating

    • Application.ScreenUpdating = True resumes screen updating.

When this procedure is executed, the station names in the text within the selected cell range are anonymized.

Excel VBA Reference | Microsoft Learn
Watch the YouTube video for this article


キーワード

#excel #できること #vba #駅名 #伏字 #正規表現 #テキスト操作 #セル範囲 #エラー処理 #パフォーマンス向上 #オブジェクト作成 #パターン設定 #テキスト検索 #VBAプロシージャ #画面更新 #Excel操作 #初心者向け #オートメーション #プログラミング #ビジネス活用

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