見出し画像

人気だけを赤字にするよ

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

このVBAプロシージャ「人気だけを赤字にするよ」は、Excelのセルに入力されているテキストの中で「人気」という文字を見つけ、その部分だけを赤字にするものです。

プロシージャの動きの流れ

  1. 画面更新の停止:

    • `Application.ScreenUpdating = False` で処理中に画面がチラつかないようにします。

  2. 変数の準備:

    • `myRng` という変数にセル範囲を、`myStr` という変数に現在チェック中の文字列を格納します。また、`i` と `a` は数値のカウンターとして使います。

  3. エラーハンドリング:

    • `On Error Resume Next` は、エラーが発生しても無視して次の行に進む設定です。

  4. 選択範囲内のセルを順番にチェック:

    • `For Each myRng In Selection` で選択したセル範囲内のセルを一つずつ確認します。

  5. セル内の文字列を順番にチェック:

    • `For i = 1 To Len(myRng)` でセル内の文字を先頭から2文字ずつ取り出し、`Mid` 関数でその部分の文字列を `myStr` に代入します。

  6. 「人気」を見つけたら赤字に変更:

    • `If myStr Like "人気"` で「人気」という文字列を見つけたら、`With myRng.Characters(Start:=i, Length:=2).Font` の部分で、その文字列を赤色 (ColorIndex = 3) に設定します。

  7. 画面更新の再開:

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

これにより、選択したセルの中で「人気」という言葉が含まれている部分だけが赤字になります。

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, 2)
            If myStr Like "人気" Then
                With myRng.Characters(Start:=i, Length:=2).Font
                    .ColorIndex = 3
                End With
            End If
        Next i
    Next myRng
    Application.ScreenUpdating = True
End Sub

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


#excel #できること #vba #文字列操作 #色変更 #セルフォーマット #文字色変更 #条件付き書式 #エクセル自動化 #人気キーワード #選択範囲 #コード解説 #画面更新 #プログラミング初心者 #エクセルマクロ #VBAコード #カスタムフォーマット #プログラミング女子 #業務効率化 #エラーハンドリング


英語翻訳

Highlighting Only the Word "人気" in Red

This explanation is created by ChatGPT.

The VBA procedure "Highlighting Only the Word '人気' in Red" is designed to search through the text in selected Excel cells, find the word "人気" (which means "popularity" in Japanese), and change its font color to red.

How the Procedure Works

  1. Stopping Screen Updating:

    • `Application.ScreenUpdating = False` is used to prevent screen flickering during the process.

  2. Preparing Variables:

    • `myRng` is used to store the cell range, `myStr` stores the current string being checked, and `i` and `a` are numeric counters.

  3. Error Handling:

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

  4. Checking Each Cell in the Selected Range:

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

  5. Checking Each Substring in the Cell:

    • `For i = 1 To Len(myRng)` extracts two characters at a time from the cell's text using the `Mid` function, storing this substring in `myStr`.

  6. Changing the Font Color of "人気":

    • If the substring matches "人気", `If myStr Like "人気"`, the font color is set to red (ColorIndex = 3) using `With myRng.Characters(Start:=i, Length:=2).Font`.

  7. Resuming Screen Updating:

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

As a result, only the instances of the word "人気" in the selected cells are highlighted in red.


Excel VBA Reference | Microsoft Learn
YouTube video for this article


#excel #whatyoucando #vba #stringmanipulation #colorchange #cellformatting #textcolorchange #conditionalformatting #excelautomation #popularkeywords #selectionrange #codeexplanation #screenupdating #programmingforbeginners #excelmacro #VBAcode #customformatting #womeninprogramming #workefficiency #errorhandling

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