見出し画像

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

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

このプロシージャは、Excelのセルに記載されている「徒歩○分」の情報を取り出し、その数値部分を隣のセルに表示するためのものです。

仕組みの説明

  1. 画面更新の停止: `Application.ScreenUpdating = False` により、コードが実行されている間の画面更新を停止して、処理速度を向上させます。

Application.ScreenUpdating = False
  1. 正規表現オブジェクトの作成: `CreateObject("VBScript.RegExp")` を使って正規表現のオブジェクトを作成します。

Dim reg As Object
Set reg = CreateObject("VBScript.RegExp") 'オブジェクト作成
  1. 正規表現の設定:

    • Pattern: "徒歩\d.*分" は「徒歩」で始まり、「分」で終わる文字列を探します。

    • IgnoreCase: 大文字と小文字を区別しない設定です。

    • Global: 一致するすべての文字列を検索します。

With reg
    .Pattern = "徒歩\d.*分"
    .IgnoreCase = True
    .Global = True
End With
  1. セルのループ処理: `Selection` で選択されたすべてのセルをループして処理します。

For Each myRng In Selection
    txt = myRng.Value
    Set match = reg.Execute(txt)
        myRng.Offset(0, 1).Value = Replace(Replace(match(0).Value, "徒歩", ""), "分", "") ' マッチしたテキストを隣の列に出力
Next myRng
  1. 一致するテキストの抽出と置換:

    • 各セルの値を取得し、正規表現で「徒歩○分」を探します。

    • 見つかった場合、「徒歩」と「分」を取り除いて数値部分だけを隣のセルに表示します。

myRng.Offset(0, 1).Value = Replace(Replace(match(0).Value, "徒歩", ""), "分", "")
  1. 画面更新の再開: `Application.ScreenUpdating = True` により、画面更新を再開します。

Application.ScreenUpdating = True

このプロシージャを実行することで、選択したセルに含まれる「徒歩○分」という情報の数値部分を簡単に隣のセルに表示できます。


キーワード

#excel #できること #vba #徒歩分数 #正規表現 #VBAオブジェクト #セル操作 #テキスト抽出 #画面更新停止 #プログラミング初心者 #Excelマクロ #セル選択 #データ処理 #テキスト置換 #VBScript #VBA入門 #隣の列 #オブジェクト作成 #コード解説 #テキスト操作


Access Walking Minutes to Adjacent Column

This explanation is created with ChatGPT.

This procedure is designed to extract "walking minutes" information from the content of an Excel cell and display the numeric part in the adjacent cell.

Explanation of the Mechanism

  1. Stop Screen Updating: `Application.ScreenUpdating = False` stops the screen updating while the code is running, improving processing speed.

Application.ScreenUpdating = False
  1. Create Regular Expression Object: Using `CreateObject("VBScript.RegExp")` to create a regular expression object.

Dim reg As Object
Set reg = CreateObject("VBScript.RegExp") 'Create object
  1. Set Regular Expression:

    • Pattern: "徒歩\d.*分" searches for strings starting with "徒歩" and ending with "分".

    • IgnoreCase: This setting makes the search case-insensitive.

    • Global: This ensures all matching strings are found.

With reg
    .Pattern = "徒歩\d.*分"
    .IgnoreCase = True
    .Global = True
End With
  1. Loop through Selected Cells: Process all selected cells with `Selection`.

For Each myRng In Selection
    txt = myRng.Value
    Set match = reg.Execute(txt)
        myRng.Offset(0, 1).Value = Replace(Replace(match(0).Value, "徒歩", ""), "分", "") ' Output matched text to adjacent column
Next myRng
  1. Extract and Replace Matching Text:

    • Retrieve the value of each cell and search for "徒歩○分" using the regular expression.

    • If found, remove "徒歩" and "分", and display only the numeric part in the adjacent cell.

myRng.Offset(0, 1).Value = Replace(Replace(match(0).Value, "徒歩", ""), "分", "")
  1. Resume Screen Updating: `Application.ScreenUpdating = True` resumes screen updating.

Application.ScreenUpdating = True

By running this procedure, you can easily display the numeric part of the "walking minutes" information found in selected cells in the adjacent cells.


Keywords

#excel #whatyoucando #vba #walkingminutes #regex #VBAobject #celloperations #textextraction #stopscreenupdating #beginnersprogramming #Excelmacro #cellselection #dataprocessing #textreplacement #VBScript #VBAintro #adjacentcolumn #objectcreation #codeexplanation #textoperations

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