見出し画像

試用期間の日間だけ隣列にだすよ

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

このVBAプロシージャは、Excelのセルに含まれるテキストから「〇〇日間」という形式の日数情報を抽出し、その結果を隣の列に出力するものです。

仕組みの説明

  1. 画面更新の停止: プロシージャの最初で `Application.ScreenUpdating = False` とすることで、処理中の画面の更新を一時的に停止し、動作を速くします。

  2. 正規表現オブジェクトの作成: `CreateObject("VBScript.RegExp")` で正規表現オブジェクトを作成します。このオブジェクトを使って特定のパターン(この場合、「〇〇日間」)を探します。

  3. 正規表現の設定:

    • `Pattern` プロパティで検索パターンを設定します。この場合、`\d{1,3}日間` で1から3桁の数字に続く「日間」という文字列を意味します。

    • `IgnoreCase` プロパティを `True` に設定して、大文字と小文字を区別しないようにします。

    • `Global` プロパティを `True` に設定して、テキスト全体で一致するすべてのパターンを探します。

  4. セルの選択範囲をループ:

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

    • 各セルの値を `txt` に代入し、そのテキストに対して正規表現を実行します。

    • 一致するパターンが見つかった場合、その結果を隣のセルに出力します。これを `myRng.Offset(0, 1).Value = match(0).Value` で行います。

  5. エラーハンドリング: `On Error Resume Next` により、エラーが発生しても処理を続行します。

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

このプロシージャを使うと、選択したセル範囲から「〇〇日間」という形式の日数情報を簡単に抽出し、隣のセルに自動で入力できます。

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


How to extract the number of days and put it in the adjacent column

This explanation is created with ChatGPT.

This VBA procedure extracts day information in the format of "〇〇日間" from the text in Excel cells and outputs the result to the adjacent column.

Explanation of the mechanism

  1. Stop screen updating: At the beginning of the procedure, `Application.ScreenUpdating = False` stops screen updating temporarily, speeding up the process.

  2. Create regular expression object: `CreateObject("VBScript.RegExp")` creates a regular expression object. This object is used to find a specific pattern (in this case, "〇〇日間").

  3. Set regular expression:

    • The `Pattern` property sets the search pattern. In this case, `\d{1,3}日間` means a 1-3 digit number followed by "日間".

    • The `IgnoreCase` property is set to `True` to ignore case.

    • The `Global` property is set to `True` to find all matches in the text.

  4. Loop through the selected cell range:

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

    • The value of each cell is assigned to `txt`, and the regular expression is executed on that text.

    • If a matching pattern is found, the result is output to the adjacent cell with `myRng.Offset(0, 1).Value = match(0).Value`.

  5. Error handling: `On Error Resume Next` allows the process to continue even if an error occurs.

  6. Resume screen updating: Finally, `Application.ScreenUpdating = True` resumes screen updating.

Using this procedure, you can easily extract day information in the format of "〇〇日間" from the selected cell range and automatically input it into the adjacent cell.

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


キーワード:
#excel #できること #vba #正規表現 #隣列出力 #セル処理 #オブジェクト作成 #パターン検索 #日数抽出 #選択範囲 #エラーハンドリング #画面更新停止 #スクリーン更新再開 #セルループ #セル値取得 #正規表現設定 #VBA解説 #初心者向け #自動入力 #テキスト処理

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