見出し画像

徒歩という漢字があったらそれだけ青色にするよ

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

このプロシージャは、選択したセル範囲の中で「徒歩」という文字が含まれている場合、その文字を青色にするものです。具体的には、各セルの文字列を2文字ずつ確認し、「徒歩」という文字列が見つかると、その部分の文字色を青色に変更します。


詳しい説明

  1. 画面更新の停止: 処理速度を向上させるために、最初に `Application.ScreenUpdating = False` として画面の更新を一時的に停止します。

  2. 変数の宣言: 使用する変数を宣言します。`myRng` はセル範囲、`myStr` は文字列、`i` はインデックス、`a` はループ用の変数です。

  3. セルのループ処理: `For Each myRng In Selection` により、選択範囲内の各セルを1つずつ処理します。

  4. 文字列の確認: 各セル内の文字列を2文字ずつ確認します。`For i = 1 To Len(myRng)` のループでセルの文字数分だけ繰り返し、`Mid` 関数を使って2文字を抽出します。

  5. 条件確認と書式変更: 抽出した2文字が「徒歩」と一致するかを `If myStr Like "徒歩" Then` で確認し、一致する場合はその部分のフォント色を `ColorIndex = 5` (青色)に変更します。

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


コードの例

Sub 徒歩という漢字があったらそれだけ青色にするよ()

    Application.ScreenUpdating = False
    Dim myRng As Range
    Dim myStr As String
    Dim i As Integer
    Dim a As Long
  
    For Each myRng In Selection
        'セルの文字列を2文字ごと調べる
        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 = 5
                End With
            End If
        Next i
    Next myRng

    Application.ScreenUpdating = True
End Sub

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


キーワード

#excel #できること #vba #文字列操作 #セル範囲 #色変更 #フォント色 #選択範囲 #書式設定 #文字検索 #部分一致 #処理速度向上 #画面更新停止 #プロシージャ #vba解説 #基本操作 #初心者向け #大手企業 #プログラミング興味 #ビデオ解説


Explanation in English

Sub Procedure: Highlight "徒歩" Characters in Blue

This explanation is created with ChatGPT.

This procedure highlights any occurrence of the characters "徒歩" in the selected cell range in blue. Specifically, it checks each cell's text two characters at a time and changes the text color to blue if it finds "徒歩".


Detailed Explanation

  1. Stopping Screen Updates: The line `Application.ScreenUpdating = False` stops screen updates temporarily to speed up the process.

  2. Variable Declaration: It declares variables used in the procedure. `myRng` is for the cell range, `myStr` for the string, `i` for the index, and `a` for the loop variable.

  3. Loop Through Cells: The `For Each myRng In Selection` loop processes each cell in the selected range one by one.

  4. Check Text Strings: The inner loop `For i = 1 To Len(myRng)` checks each cell's text two characters at a time using the `Mid` function.

  5. Condition Check and Formatting: If the two characters match "徒歩", the procedure changes the font color to blue with `ColorIndex = 5`.

  6. Resuming Screen Updates: Finally, `Application.ScreenUpdating = True` resumes screen updates.


Code Example

Sub 徒歩という漢字があったらそれだけ青色にするよ()

    Application.ScreenUpdating = False
    Dim myRng As Range
    Dim myStr As String
    Dim i As Integer
    Dim a As Long
  
    For Each myRng In Selection
        'セルの文字列を2文字ごと調べる
        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 = 5
                End With
            End If
        Next i
    Next myRng

    Application.ScreenUpdating = True
End Sub

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


Keywords

#excel #できること #vba #文字列操作 #セル範囲 #色変更 #フォント色 #選択範囲 #書式設定 #文字検索 #部分一致 #処理速度向上 #画面更新停止 #プロシージャ #vba解説 #基本操作 #初心者向け #大手企業 #プログラミング興味 #ビデオ解説

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