見出し画像

未経験を赤字に経験者を青字するよ

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

このプロシージャは、「未経験」 という言葉が含まれている部分を赤色にし、「経験」 という言葉が含まれている部分を青色に変更するという機能を持っています。

基本的な流れとしては、選択したセルの中にある文字列に対して、「正規表現」 という方法を使い、特定の単語を見つけ出し、その単語だけを色分けしています。

プロシージャの動作の説明:

  1. 画面の更新を止める
    プロシージャの最初で `Application.ScreenUpdating = False` として、Excelの画面が処理中に何度も更新されないようにしています。これにより、処理が少し速くなります。

  2. 正規表現のオブジェクトを作成
    `reg` と `reg2` という2つの正規表現オブジェクトを作成しています。この正規表現は、特定のパターンに基づいてテキスト内の文字列を探すことができます。

    • `reg` は「経験者」や「経験」という言葉を探します。

    • `reg2` は「未経験者」や「未経験」という言葉を探します。

  3. セル内の文字列を検索
    選択した範囲の各セルに対して、その中の文字列を `reg` と `reg2` で調べます。

    • 「経験者」や「経験」 が見つかったら、その文字を青色にします。

    • 「未経験者」や「未経験」 が見つかったら、その文字を赤色にします。

  4. 色をつける処理
    見つけた単語の部分だけ色を変えます。それぞれの単語の位置と長さを取得し、その範囲に対して色を変更します。

  5. 画面の更新を再開する
    最後に、`Application.ScreenUpdating = True` で、画面の更新を再び有効にしています。

ポイント:

  • 「未経験」 という言葉は赤色、「経験」 という言葉は青色になります。

  • 一度に複数のセルを選択して、そのセルの中にある特定の単語だけを色分けできる便利なツールです。


Sub 未経験を赤字に経験者を青字するよ()
    Application.ScreenUpdating = False
    Dim myRng As Range
    Dim myStr, txt As String
    Dim reg, reg2 As Object
    Set reg = CreateObject("VBScript.RegExp") 'オブジェクト作成
    Set reg2 = CreateObject("VBScript.RegExp") 'オブジェクト作成
    Dim matches, matches2, match, match2 As Object
    On Error Resume Next
    With reg
        .Pattern = "[?<!未)経験(者)?]"
        .IgnoreCase = True
        .Global = True
    End With
    With reg2
        .Pattern = "未経験(者)?"
        .IgnoreCase = True
        .Global = True
    End With
    For Each myRng In Selection
        txt = myRng.Value
        Set matches = reg.Execute(txt)
        If matches.count > 0 Then
            'マッチした文字列の部分だけ赤色にする
            For Each match In matches
                myRng.Characters(match.FirstIndex + 1, match.Length).Font.Color = vbBlue
            Next match
        End If
        Set matches2 = reg2.Execute(txt)
        If matches2.count > 0 Then
            'マッチした文字列の部分だけ青色にする
            For Each match2 In matches2
                myRng.Characters(match2.FirstIndex + 1, match2.Length).Font.Color = vbRed
            Next match2
        End If
    Next myRng
    Application.ScreenUpdating = True
End Sub

ハッシュタグ:

#excel #vba #正規表現 #文字列検索 #色分け #セル内文字 #フォントカラー変更 #未経験 #経験者 #プログラミング #自動化 #オフィス作業 #効率化 #初心者向け #条件付き書式 #データ処理 #選択範囲 #作業スピードアップ #テキスト編集 #できること


English Translation

Make "未経験" Red and "経験者" Blue

This explanation is created by ChatGPT.

This procedure has the function of changing the color of the word "未経験" (inexperienced) to red and "経験" (experienced) to blue.

How the procedure works:

  1. Stopping screen updates
    The procedure starts by setting `Application.ScreenUpdating = False` to prevent the Excel screen from updating during the process, speeding up the execution.

  2. Creating Regular Expression Objects
    Two regular expression objects, `reg` and `reg2`, are created. These objects help search for specific patterns in the text.

    • `reg` looks for words like "経験者" (experienced person) or "経験" (experience).

    • `reg2` looks for words like "未経験" (inexperienced) or "未経験者" (inexperienced person).

  3. Searching the Text in Each Cell
    The procedure goes through each selected cell and checks its contents using `reg` and `reg2`.

    • If it finds "経験" or "経験者", it changes those words to blue.

    • If it finds "未経験" or "未経験者", it changes those words to red.

  4. Changing the Color of the Text
    The matching words' position and length are determined, and then only that portion of the text is colored accordingly.

  5. Resuming Screen Updates
    Finally, screen updating is turned back on using `Application.ScreenUpdating = True`.

Key Points:

  • Words like "未経験" turn red, while "経験" turns blue.

  • You can select multiple cells at once and apply color changes to specific words within the text in those cells.



Hashtags:

#excel #vba #regex #textsearch #textcoloring #celltext #fontcolorchange #inexperienced #experienced #automation #officetasks #efficiency #beginnerfriendly #conditionalformatting #dataprocessing #range #speedup #textediting #thingsyoucando

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