見出し画像

アクセスのバス分数だけ隣列にだすよ

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

このプロシージャは、Excelシートで選択したセルの中から「バス〇〇分」というテキストを見つけ、その分数部分だけを隣の列に表示するものです。以下に、具体的な仕組みを説明します。

  1. Application.ScreenUpdating = False

    • これは、処理中に画面がちらつくのを防ぐための設定です。処理が終わったら True に戻します。

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

    • `CreateObject("VBScript.RegExp")` を使って、正規表現オブジェクトを作成します。このオブジェクトを使って特定のパターンに一致するテキストを探します。

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

    • `.Pattern = "バス\d.*分"` は、「バス〇〇分」というテキストパターンを表します。

    • `.IgnoreCase = True` は大文字と小文字を区別しない設定です。

    • `.Global = True` は、セル内のすべての一致を見つける設定です。

  4. エラーハンドリング

    • `On Error Resume Next` によって、エラーが発生してもプログラムが中断しないようにします。

  5. セルのループ処理

    • `For Each myRng In Selection` で、選択範囲のセルを1つずつ処理します。

    • `txt = myRng.Value` で、セルの値を取得します。

    • `Set match = reg.Execute(txt)` で、正規表現パターンに一致するテキストを探します。

    • `myRng.Offset(0, 1).Value = Replace(Replace(match(0).Value, "バス", ""), "分", "")` で、見つけたテキストから「バス」と「分」を取り除き、分数部分だけを隣の列に出力します。

  6. Application.ScreenUpdating = True

    • 最後に、画面更新を再開します。

これにより、選択したセルの中から「バス〇〇分」の分数部分だけを隣の列に簡単に表示することができます。

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


Extracting Bus Minutes to Adjacent Column

This explanation is created with ChatGPT.

This procedure identifies the "バス〇〇分" (Bus xx minutes) pattern within the selected cells in an Excel sheet and extracts the numerical part (minutes) to display in the adjacent column. Here's a detailed breakdown of how it works:

  1. Disable Screen Updating

    • `Application.ScreenUpdating = False` prevents the screen from flickering during the process. It is reset to True at the end.

  2. Create Regular Expression Object

    • `CreateObject("VBScript.RegExp")` creates a RegExp object, used for finding text patterns.

  3. Set Regular Expression Pattern

    • `.Pattern = "バス\d.*分"` sets the pattern to match "Bus xx minutes".

    • `.IgnoreCase = True` makes the search case-insensitive.

    • `.Global = True` searches for all occurrences in the text.

  4. Error Handling

    • `On Error Resume Next` ensures the program continues running even if an error occurs.

  5. Loop Through Selected Cells

    • `For Each myRng In Selection` processes each selected cell.

    • `txt = myRng.Value` gets the cell's value.

    • `Set match = reg.Execute(txt)` searches for the text matching the pattern.

    • `myRng.Offset(0, 1).Value = Replace(Replace(match(0).Value, "バス", ""), "分", "")` extracts the minutes part, removing "Bus" and "minutes", and outputs it to the adjacent cell.

  6. Re-enable Screen Updating

    • `Application.ScreenUpdating = True` resumes screen updates.

This allows you to easily display the bus minutes in the adjacent column from the selected cells.

Excel VBA Reference | Microsoft Learn
YouTube video for this article


キーワード:
#excel #できること #vba #バス #分数 #正規表現 #セル #隣の列 #エラーハンドリング #画面更新 #オブジェクト作成 #テキスト抽出 #パターンマッチ #VBScript #Range #選択範囲 #ループ処理 #コード解説 #初心者向け #Excelマクロ

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