見出し画像

業務改善:先頭が3から始まるセルを一瞬で赤くするExcelテクニック

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

このプロシージャは、選択されたセルの中から値が*「3」*で始まるものだけを見つけ出し、そのセルの背景色を赤色に変えるものです。具体的にどのように動作するかを説明します。

プロシージャの仕組み

  1. 画面の更新を停止

    • `Application.ScreenUpdating = False` により、プロシージャが実行されている間の画面更新を一時停止し、処理速度を上げています。

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

    • `CreateObject("VBScript.RegExp")` によって正規表現を使うためのオブジェクトを作成しています。

    • `With reg ... End With` の中で、正規表現のパターンを `^3` と指定しています。これにより、文字列の先頭が「3」であるかをチェックする設定をしています。

  3. エラーハンドリング

    • `On Error Resume Next` により、エラーが発生しても処理を続行するようにしています。

  4. セルの値をチェック

    • `For Each myRng In Selection` によって、選択されたセル範囲を一つずつチェックします。

    • `If reg.test(txt) Then` で、セルの値が「3」で始まるかを正規表現でテストしています。

  5. セルの背景色を変更

    • `myRng.Interior.ColorIndex = 3` によって、条件を満たすセルの背景色を赤色に変更しています。

  6. 画面の更新を再開

    • `Application.ScreenUpdating = True` によって、画面更新を再開しています。

詳細なコード解説

以下は、各部分のコードを簡単に説明したものです。

Sub 先頭が3から始まるセルだけを赤色にするよ()
    Application.ScreenUpdating = False   '画面更新を停止
    
    Dim reg
    Set reg = CreateObject("VBScript.RegExp")   '正規表現オブジェクトの作成
    Dim myRng As Range
    Dim txt As String
    Dim i As Long
    
    With reg
        .Pattern = "^3"   'パターンの設定:先頭が「3」
        .IgnoreCase = True   '大文字・小文字を区別しない
        .Global = True
    End With
    
    On Error Resume Next   'エラーハンドリング
    
    For Each myRng In Selection   '選択範囲のセルを一つずつチェック
        txt = myRng.Value
        If reg.test(txt) Then   'セルの値が「3」で始まるかをチェック
            myRng.Interior.ColorIndex = 3   '条件を満たすセルを赤色に変更
        End If
    Next myRng
    
    Application.ScreenUpdating = True   '画面更新を再開
End Sub

まとめ

このプロシージャは、Excelの選択範囲内で値が「3」で始まるセルを見つけ、そのセルの背景色を赤色に変更するというものです。正規表現を使って効率的にセルの値をチェックし、条件を満たすセルだけに変更を加えています。


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


#excel #できること #vba #セルの色 #背景色変更 #正規表現 #選択範囲 #セルの値 #条件付き書式 #自動化 #マクロ #スクリプト #プログラミング初心者 #データ操作 #Excelマクロ #VBAチュートリアル #初心者向け #オフィス作業効率化 #Excel自動化 #Excelスキル


English Translation

Change the Color of Cells Starting with 3 to Red

This explanation is created using ChatGPT.

This procedure finds cells within the selected range that start with "3" and changes their background color to red. Here’s how it works:

How the Procedure Works

  1. Stop Screen Updating

    • `Application.ScreenUpdating = False` temporarily stops screen updates during the procedure to increase processing speed.

  2. Create a Regular Expression Object

    • `CreateObject("VBScript.RegExp")` creates an object to use regular expressions.

    • Inside `With reg ... End With`, the regular expression pattern is set to `^3`, which checks if the string starts with "3".

  3. Error Handling

    • `On Error Resume Next` continues processing even if an error occurs.

  4. Check Cell Values

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

    • `If reg.test(txt) Then` tests if the cell value starts with "3" using the regular expression.

  5. Change Cell Background Color

    • `myRng.Interior.ColorIndex = 3` changes the background color of cells that meet the condition to red.

  6. Resume Screen Updating

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

Detailed Code Explanation

Here is a simplified explanation of each part of the code:

Sub 先頭が3から始まるセルだけを赤色にするよ()
    Application.ScreenUpdating = False   'Stop screen updates
    
    Dim reg
    Set reg = CreateObject("VBScript.RegExp")   'Create regular expression object
    Dim myRng As Range
    Dim txt As String
    Dim i As Long
    
    With reg
        .Pattern = "^3"   'Set pattern to check if string starts with "3"
        .IgnoreCase = True   'Ignore case
        .Global = True
    End With
    
    On Error Resume Next   'Error handling
    
    For Each myRng In Selection   'Check each cell in the selected range
        txt = myRng.Value
        If reg.test(txt) Then   'Check if cell value starts with "3"
            myRng.Interior.ColorIndex = 3   'Change the background color to red
        End If
    Next myRng
    
    Application.ScreenUpdating = True   'Resume screen updates
End Sub

Summary

This procedure finds cells in the selected range that start with "3" and changes their background color to red. It uses regular expressions to efficiently check cell values and only changes cells that meet the condition.


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


#excel #できること #vba #cellcolor #backgroundcolorchange #regex #selectedrange #cellvalue #conditionalformatting #automation #macro #script #programmingbeginner #datahandling #excelmacro #VBAtutorial #beginnerfriendly #officeefficiency #excelautomation #excelskills

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