見出し画像

活躍中だけを赤字にするよ

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

このプロシージャは、選択したセルの中で「活躍中」という文字が含まれている部分だけを赤色にするためのものです。たとえば、セルに「彼は現在活躍中です」という文字が含まれている場合、その「活躍中」の部分だけが赤く表示されるようになります。

プロシージャの仕組み

  1. 画面更新の停止:
    プロシージャの最初と最後に、`Application.ScreenUpdating`を使って画面の更新を停止および再開しています。これにより、コードの実行中に画面がちらつくのを防ぎ、処理を少し速くします。

  2. 変数の設定:

    • `myRng`: 選択したセル範囲を指します。

    • `myStr`: セル内の文字列を1文字ずつ取り出して「活躍中」と一致するか確認するために使います。

    • `i`: 文字列を一文字ずつチェックするためのループカウンターです。

  3. セルの文字を1文字ずつ確認:
    `For Each`ループで選択したセル範囲のそれぞれのセルに対して処理を行います。さらに`For`ループを使って、そのセル内の文字を一文字ずつ確認し、3文字ずつ切り出して「活躍中」という文字列と一致するかどうかをチェックします。

  4. 色の変更:
    一致した場合、`With myRng.Characters(Start:=i, Length:=3).Font`を使って、その3文字のフォントカラーを赤色(`ColorIndex = 3`)に変更します。

  5. エラーハンドリング:
    `On Error Resume Next`により、エラーが発生してもプロシージャ全体が止まらないようにしていますが、この部分は場合によっては注意が必要です。

このコードを実行すると、選択したセルの中に「活躍中」という文字が含まれている場合、その部分だけが赤色に変わります。

Sub 活躍中だけを赤字にするよ()
    Application.ScreenUpdating = False
    Dim myRng As Range
    Dim myStr As String
    Dim i As Integer
    Dim a As Long
    On Error Resume Next
    For Each myRng In Selection
        For i = 1 To Len(myRng)
            myStr = Mid(myRng.Value, i, 3)
            If myStr Like "活躍中" Then
                With myRng.Characters(Start:=i, Length:=3).Font
                    .ColorIndex = 3
                End With
            End If
        Next i
    Next myRng
    Application.ScreenUpdating = True
End Sub

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


ハッシュタグ

#excel #できること #vba #セル書式 #フォントカラー変更 #文字列操作 #エラーハンドリング #色変更 #アクティブセル #ループ処理 #条件分岐 #コード解説 #初心者向け #プログラミング #効率化 #自動化 #データ処理 #Excelスキル #Excel学習 #VBAマクロ


英語での説明

Highlight Only the Word "活躍中" in Red

This explanation was created by ChatGPT.

This procedure is designed to highlight only the word "活躍中" in red within the selected cells. For example, if a cell contains the text "彼は現在活躍中です", only the "活躍中" part will be displayed in red.

How the Procedure Works

  1. Disabling Screen Updating:
    At the beginning and end of the procedure, `Application.ScreenUpdating` is used to stop and then restart screen updating. This prevents screen flickering during the execution of the code and makes the process slightly faster.

  2. Variable Setup:

    • `myRng`: Refers to the selected cell range.

    • `myStr`: Used to extract and check substrings of the cell value, comparing them to the word "活躍中".

    • `i`: A loop counter for checking each character in the cell one by one.

  3. Checking Each Character in the Cell:
    Using a `For Each` loop, the code processes each cell within the selected range. Then, an additional `For` loop is used to examine the cell's content, extracting three characters at a time and comparing them to the word "活躍中".

  4. Changing the Color:
    If a match is found, the code uses `With myRng.Characters(Start:=i, Length:=3).Font` to change the font color of those three characters to red (indicated by `ColorIndex = 3`).

  5. Error Handling:
    `On Error Resume Next` ensures that if an error occurs, the procedure doesn't halt entirely. However, this approach should be used cautiously, as it can sometimes mask issues that need attention.

When you run this code, any instance of the word "活躍中" within the selected cells will be highlighted in red.


Excel VBA Reference | Microsoft Learn
Watch the YouTube video related to this article here


Hashtags

#excel #whatyoucando #vba #cellformatting #fontcolorchange #stringmanipulation #errorhandling #colorchange #activecell #loopprocessing #conditionalbranching #codeexplanation #forbeginners #programming #efficiency #automation #dataprocessing #excelskills #learningexcel #vbamacros

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