見出し画像

高時給だけを赤字にするよ

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

このVBAプロシージャ「高時給だけを赤字にするよ」は、Excelのセル内で「高時給」という文字列が含まれている部分だけを自動的に赤色に変えるためのものです。以下に、このプロシージャの仕組みをわかりやすく説明します。


  1. 画面更新を一時停止
    コードの最初で、`Application.ScreenUpdating = False` と記述することで、マクロ実行中の画面更新を一時的に停止します。これにより、処理速度が速くなり、画面のちらつきを防ぐことができます。

  2. 変数の宣言

    • `myRng` は、選択したセル範囲の中の1つ1つのセルを表します。

    • `myStr` は、セル内の文字列の一部分を取り出して一時的に保存するための変数です。

    • `i` は、セル内の文字の位置を示すためのカウンタです。

  3. エラーハンドリング
    `On Error Resume Next` というコードにより、エラーが発生してもプロシージャが途中で止まることなく、次の行の処理に進むようになります。

  4. セルのループ処理
    `For Each myRng In Selection` の部分で、選択したセル範囲内の各セルについて順番に処理を行います。

  5. 文字列の部分一致検索
    `For i = 1 To Len(myRng)` というループで、セル内の文字列を1文字ずつチェックし、3文字ずつ取り出して確認します。`Mid(myRng.Value, i, 3)` で文字列の一部分を取り出します。

  6. 文字列の比較と色の変更
    `If myStr Like "高時給" Then` で、取り出した3文字が「高時給」と一致する場合に、その文字列を赤色に変えます。赤色は `ColorIndex = 3` で指定されています。

  7. 画面更新の再開
    最後に、`Application.ScreenUpdating = True` と記述することで、画面更新を再開し、処理結果が画面に反映されます。


まとめ

このプロシージャは、Excelのセル内に「高時給」という文字が含まれている場合、その部分だけを赤字に変更します。具体的には、セル内の文字列を1文字ずつ調べ、該当部分が見つかるとその文字列を赤くします。

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 #文字色変更 #エクセル自動化 #エクセル活用 #セル操作 #VBA初心者 #マクロ #業務効率化 #セルフォーマット #赤字変更 #文字列検索 #セル内検索 #エクセルマクロ #エラー処理 #プログラミング学習


English Explanation

Sub Name: Highlight "高時給" in Red

This explanation is created with ChatGPT.

This VBA procedure named Highlight "高時給" in Red is designed to automatically change the color of the text "高時給" (which means "high hourly wage" in Japanese) to red within selected cells in Excel. Here's a simplified breakdown of how it works:


  1. Pausing Screen Updates
    The procedure starts by pausing the screen updates using `Application.ScreenUpdating = False` to speed up the process and prevent screen flicker during execution.

  2. Variable Declaration

    • `myRng` is used to refer to each cell in the selected range.

    • `myStr` temporarily stores a part of the string from each cell.

    • `i` is a counter that indicates the position of characters within the cell's text.

  3. Error Handling
    The `On Error Resume Next` line allows the procedure to continue running even if an error occurs, skipping over problematic code.

  4. Looping Through Selected Cells
    The `For Each myRng In Selection` loop processes each cell within the selected range one by one.

  5. Substring Matching
    The inner loop `For i = 1 To Len(myRng)` checks each character in the cell text, extracting 3 characters at a time using `Mid(myRng.Value, i, 3)`.

  6. Comparison and Color Change
    If the extracted 3-character substring matches "高時給", the corresponding text is changed to red using `.ColorIndex = 3`.

  7. Resuming Screen Updates
    Finally, `Application.ScreenUpdating = True` resumes screen updates, showing the results of the changes on the screen.


Summary

This procedure specifically searches for the text "高時給" in selected Excel cells and changes its color to red. It examines the cell's text character by character and highlights the matching substring.


#excel #vba #cell_formatting #conditional_formatting #automation #macro #vba_tutorial #error_handling #substring_search #color_change #excel_macro #beginner_vba #text_formatting #efficiency #highlighting #screen_updating #range_selection #string_comparison #font_color #learning_vba

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