見出し画像

むずかしい高橋があったらセルを青色にするよ

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

このVBAプロシージャは、Excelの選択範囲内で文字列に特定の漢字(ここでは「髙」)が含まれているセルを青色にするものです。

プロシージャの仕組み

  1. 画面更新の停止:

    • `Application.ScreenUpdating = False` は、処理の速度を上げるために画面の更新を一時的に止めます。

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

    • `CreateObject("VBScript.RegExp")` を使って正規表現オブジェクトを作成します。このオブジェクトを使って文字列内の特定のパターンを検索します。

  3. 正規表現の設定:

    • `.Pattern = "髙"` で検索するパターンを設定します。

    • `.IgnoreCase = True` で大文字と小文字を区別しないようにします。

    • `.Global = True` で全体を検索対象とします。

  4. 選択範囲内のセルを一つずつチェック:

    • `For Each myRng In Selection` で選択範囲内の各セルをループします。

    • `txt = myRng.Value` でセルの値を取得します。

    • `If reg.Test(txt) Then` でセルの値に「髙」が含まれているかを確認します。

    • 含まれていれば `myRng.Interior.ColorIndex = 5` でセルの背景色を青色に変更します。

  5. エラー処理:

    • `On Error Resume Next` でエラーが発生してもプロシージャを続行します。

  6. 画面更新の再開:

    • `Application.ScreenUpdating = True` で画面の更新を再開します。

コードの表示

Sub むずかしい高橋があったらセルを青色にするよ()
    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 = "髙"
        .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 #セルの色変更 #正規表現 #文字列検索 #画面更新 #エラーハンドリング #プログラミング初心者 #オブジェクト作成 #範囲選択 #背景色変更 #セル操作 #テキスト処理 #大文字小文字無視 #ループ処理 #VBAチュートリアル #プログラミング学習 #VBAコード #Excel自動化


Make cells blue if they contain a difficult "Takahashi"

This explanation is created using ChatGPT.

This VBA procedure in Excel turns the cells in the selected range blue if they contain a specific kanji character (here, "髙").

How the procedure works

  1. Stop screen updating:

    • `Application.ScreenUpdating = False` temporarily stops screen updating to speed up processing.

  2. Create regular expression object:

    • `CreateObject("VBScript.RegExp")` creates a regular expression object to search for specific patterns in strings.

  3. Set up the regular expression:

    • `.Pattern = "髙"` sets the pattern to search for.

    • `.IgnoreCase = True` makes the search case-insensitive.

    • `.Global = True` ensures the search is applied globally.

  4. Check each cell in the selected range:

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

    • `txt = myRng.Value` retrieves the cell's value.

    • `If reg.Test(txt) Then` checks if the cell's value contains "髙".

    • If it does, `myRng.Interior.ColorIndex = 5` changes the cell's background color to blue.

  5. Error handling:

    • `On Error Resume Next` allows the procedure to continue even if an error occurs.

  6. Resume screen updating:

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

Code Display

Sub むずかしい高橋があったらセルを青色にするよ()
    Application.ScreenUpdating = False
    
    Dim reg
    Set reg = CreateObject("VBScript.RegExp")   'Create Object
    Dim myRng As Range
    Dim txt As String
    Dim i As Long
    With reg
        .Pattern = "髙"
        .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

Reference Links

Related Keywords

#excel #whatitcando #vba #changecellcolor #regex #searchstring #screenupdating #errorhandling #programmingbeginner #createobject #rangeselection #backgroundcolorchange #celloperations #textprocessing #caseinsensitive #loopprocessing #VBAtutorial #learnprogramming #VBAcode #Excelautomation

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