見出し画像

アクセス所要分数だけを隣列にだすよ

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

このプロシージャは、選択したセルのテキストから「○○分」という形式の数字を抽出し、その数字を隣の列に表示するものです。

仕組みの説明

  1. 画面更新の停止
    ```vba
    Application.ScreenUpdating = False
    ```
    画面の更新を一時停止して、処理速度を向上させます。

  2. 正規表現オブジェクトの作成
    ```vba
    Dim reg As Object
    Set reg = CreateObject("VBScript.RegExp") 'オブジェクト作成
    ```
    正規表現を使うためのオブジェクトを作成します。

  3. 変数の宣言
    ```vba
    Dim myRng As Range
    Dim txt As String
    Dim match As Object
    Dim i As Long
    ```

  4. 正規表現の設定
    ```vba
    With reg
    .Pattern = "\d.*分"
    .IgnoreCase = True
    .Global = True
    End With
    ```
    この正規表現は、数字と「分」の組み合わせを探します。

  5. 選択範囲の各セルについての処理
    ```vba
    For Each myRng In Selection
    txt = myRng.Value
    Set match = reg.Execute(txt)
    myRng.Offset(0, 1).Value = Replace(match(0).Value, "分", "")
    Next myRng
    ```
    - 各セルの値を取得します。
    - 正規表現を使って「○○分」を抽出します。
    - 抽出したテキストから「分」を取り除いて、隣のセルに表示します。

  6. 画面更新の再開
    ```vba
    Application.ScreenUpdating = True
    ```
    最後に画面の更新を再開します。

これで、選択したセルの「○○分」を隣の列に数字だけで表示できます。


Excel VBA リファレンス | Microsoft Learn

この記事のYouTube動画はこちら


キーワード:
#excel #できること #vba #時間抽出 #正規表現 #オブジェクト作成 #セル操作 #列操作 #データ抽出 #画面更新停止 #VBScript #正規表現オブジェクト #テキスト抽出 #分抽出 #隣の列 #値の置換 #VBA初心者 #業務効率化 #Excelマクロ #セル選択


Extract Travel Minutes

This explanation is created with ChatGPT.

This procedure extracts the number of minutes in the format "xx分" from the selected cells and displays the number in the adjacent column.

Explanation of the Mechanism

  1. Stop Screen Updating
    ```vba
    Application.ScreenUpdating = False
    ```
    Temporarily stops screen updating to speed up the process.

  2. Create Regular Expression Object
    ```vba
    Dim reg As Object
    Set reg = CreateObject("VBScript.RegExp") 'Create Object
    ```
    Creates an object to use regular expressions.

  3. Declare Variables
    ```vba
    Dim myRng As Range
    Dim txt As String
    Dim match As Object
    Dim i As Long
    ```

  4. Set Regular Expression
    ```vba
    With reg
    .Pattern = "\d.*分"
    .IgnoreCase = True
    .Global = True
    End With
    ```
    This regular expression looks for combinations of numbers and "分".

  5. Process Each Cell in the Selection
    ```vba
    For Each myRng In Selection
    txt = myRng.Value
    Set match = reg.Execute(txt)
    myRng.Offset(0, 1).Value = Replace(match(0).Value, "分", "")
    Next myRng
    ```
    - Retrieves the value of each cell.
    - Uses the regular expression to extract "xx分".
    - Removes "分" from the extracted text and displays the number in the adjacent cell.

  6. Resume Screen Updating
    ```vba
    Application.ScreenUpdating = True
    ```
    Finally, resumes screen updating.

This allows you to display the number of minutes from the selected cells in the adjacent column.


Excel VBA Reference | Microsoft Learn

YouTube Video for this Article


Keywords:
#excel #vba #extracttime #regex #createobject #celloperation #coloperation #dataextraction #stopscreenupdating #VBScript #regexobject #textextraction #extractminutes #adjacentcolumn #replacingvalues #VBAbeginners #efficiency #Excelmacro #cellselection

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