見出し画像

同じ文字が連続していたらセルを青色にするよ

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

このVBAコードは、Excelのセルに同じ文字が連続しているかどうかをチェックし、連続している場合はそのセルの背景色を青色に変えるプログラムです。以下のように動作します。


プログラムの流れ

  1. Application.ScreenUpdating = False
    これにより、プログラムが動作している間は画面の更新を一時的に止めて、処理を早くしています。処理が終わった後に再度画面を更新します。

  2. 正規表現の設定
    `Set reg = CreateObject("VBScript.RegExp")` によって、VBScriptの正規表現オブジェクトを作成します。
    `With reg` ブロック内で、次の設定を行います。

    • `.Pattern = "(.)\1+"` : この部分では、同じ文字が2つ以上連続しているというパターンを設定しています。

    • `.IgnoreCase = True` : 大文字と小文字を区別せずにチェックします。

    • `.Global = True` : テキスト全体を対象に検索します。

  3. 選択範囲内のセルをループ処理
    `For Each myRng In Selection` により、選択されている範囲の各セルに対して処理を行います。
    例えば、範囲がA1:A5であれば、それぞれのセルA1、A2、A3...と順番にチェックします。

  4. 正規表現でチェック
    各セルの値を `txt` に格納し、`If reg.Test(txt) Then` によって、その値に同じ文字の連続が含まれているかを確認します。

  5. 条件に合えばセルを青色に
    条件に合致した場合(同じ文字が連続していた場合)、`myRng.Interior.ColorIndex = 5` により、セルの背景色が青色(ColorIndex 5)に変わります。

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


まとめ

このプログラムを使うと、選択した範囲内のセルで、同じ文字が2つ以上続いているセルを自動で青くすることができます。たとえば、「aa」や「bbb」といった文字列が含まれているセルが対象になります。


Sub 同じ文字が連続していたらセルを青色にするよ()
    Application.ScreenUpdating = False
            
    Dim reg
    Set reg = CreateObject("VBScript.RegExp")   'オブジェクト作成
    Dim myRng As Range
    Dim txt As String
    With reg
        .Pattern = "(.)\1+"
        .IgnoreCase = True
        .Global = True
    End With
    On Error Resume Next
    For Each myRng In Selection
            txt = myRng.Value
            If reg.Test(txt) Then
                myRng.Interior.ColorIndex = 5
            End If
    Next myRng
    Application.ScreenUpdating = True
End Sub

関連するハッシュタグ

#excel #できること #vba #連続文字 #セル書式設定 #条件付き書式 #正規表現 #背景色変更 #自動化 #VBScript #パターンマッチング #ループ処理 #エクセルマクロ #テキスト処理 #色設定 #プログラミング初心者 #簡単vba #エラーハンドリング #コード解説 #選択範囲処理


English Translation

Change Cell Color to Blue if the Same Character Repeats

This explanation is created with ChatGPT.

This VBA code checks if there are repeating characters in any of the selected cells in Excel and changes the background color to blue if repeats are found. Here's how it works:


Program Flow

  1. Application.ScreenUpdating = False
    This temporarily stops screen updates while the program is running, making the process faster. Screen updates resume after the process completes.

  2. Setting up Regular Expressions
    `Set reg = CreateObject("VBScript.RegExp")` creates a VBScript Regular Expression object.
    Inside the `With reg` block, the following settings are made:

    • `.Pattern = "(.)\1+"` : This pattern matches any character that repeats two or more times.

    • `.IgnoreCase = True` : Ignores case when checking.

    • `.Global = True` : Searches through the entire text.

  3. Loop through the Selected Range
    `For Each myRng In Selection` processes each cell in the selected range.
    For example, if the range is A1:A5, it checks cells A1, A2, A3, etc.

  4. Check with Regular Expressions
    The cell value is stored in `txt`, and `If reg.Test(txt) Then` checks if there are repeating characters in that value.

  5. Color the Cell Blue if Condition is Met
    If the condition is met (repeating characters are found), `myRng.Interior.ColorIndex = 5` changes the background color of the cell to blue (ColorIndex 5).

  6. Application.ScreenUpdating = True
    Finally, screen updates are resumed.


Summary

Using this program, you can automatically turn cells blue if they contain repeated characters within the selected range. For example, cells containing "aa" or "bbb" will be highlighted.



Related Hashtags

#excel #できること #vba #repeatingCharacters #cellFormatting #conditionalFormatting #regex #backgroundChange #automation #VBScript #patternMatching #loopProcessing #excelMacro #textProcessing #colorSettings #beginnerProgramming #easyVBA #errorHandling #codeExplanation #rangeProcessing

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