見出し画像

分数があったらセルを青色にするよ

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

このプロシージャは、選択範囲のセルに「〇分」のような分数の記述があった場合、そのセルを青色に変更します。以下に、プロシージャの仕組みをわかりやすく説明します。

  1. 画面更新を停止する
    `Application.ScreenUpdating = False`
    これは、処理の途中で画面がちらつかないようにするためです。

  2. 正規表現オブジェクトを作成する
    `Set reg = CreateObject("VBScript.RegExp")`
    ここでは、分数を見つけるために正規表現オブジェクトを作成しています。

  3. 正規表現のパターンを設定する
    `With reg`
    `.Pattern = "\d{1,2}分"`
    `.IgnoreCase = True`
    `.Global = True`
    `End With`
    これは、「1~2桁の数字+分」というパターンを探す設定です。

  4. 選択範囲のセルを一つずつ確認する
    `For Each myRng In Selection`
    `txt = myRng.Value`
    `If reg.test(txt) Then`
    `myRng.Interior.ColorIndex = 5`
    `End If`
    `Next myRng`
    選択したセルの中身を一つずつ確認し、設定した正規表現に一致する場合、そのセルの背景色を青色(ColorIndex = 5)に変更します。

  5. 画面更新を再開する
    `Application.ScreenUpdating = True`
    最後に、画面更新を再開して処理を完了します。


Translate to English

Highlight Cells with "Minutes" in Blue

This explanation is created with ChatGPT.

This procedure changes the background color of cells containing "minutes" (e.g., "15分") to blue in the selected range. Here’s a step-by-step explanation:

  1. Stop Screen Updating
    `Application.ScreenUpdating = False`
    This prevents screen flickering during the process.

  2. Create Regular Expression Object
    `Set reg = CreateObject("VBScript.RegExp")`
    This creates a regular expression object to find "minutes."

  3. Set the Regular Expression Pattern
    `With reg`
    `.Pattern = "\d{1,2}分"`
    `.IgnoreCase = True`
    `.Global = True`
    `End With`
    This pattern searches for "1 to 2 digits + 分".

  4. Check Each Cell in the Selected Range
    `For Each myRng In Selection`
    `txt = myRng.Value`
    `If reg.test(txt) Then`
    `myRng.Interior.ColorIndex = 5`
    `End If`
    `Next myRng`
    It checks each cell in the selection and changes the background color to blue (ColorIndex = 5) if it matches the pattern.

  5. Resume Screen Updating
    `Application.ScreenUpdating = True`
    Finally, it resumes screen updating to complete the process.


Links:

Keywords:
#excel #できること #vba #正規表現 #セル操作 #条件付き書式 #分数検出 #セルの色変更 #初心者向けVBA #VBA入門 #プログラミング初心者 #画面更新停止 #オブジェクト作成 #選択範囲 #背景色変更 #条件付き書式VBA #正規表現パターン #VBA解説 #自動化 #ExcelTips

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