見出し画像

社保という漢字があったらそれだけ赤色にするよ

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

このプロシージャは、Excelのセル内に「社保」という文字列が含まれている場合、その部分だけを赤色にするものです。

仕組みの説明

  1. Application.ScreenUpdating = False

    • 画面の更新を停止 します。これにより、処理の途中で画面がチラつくのを防ぎます。

  2. Dim myRng As Range, myStr As String, i As Integer

    • 変数の宣言 を行います。`myRng` はセル範囲を、`myStr` はセル内の2文字の文字列を、`i` はカウンタを表します。

  3. For Each myRng In Selection

    • 選択範囲内の各セルに対して繰り返し処理 を行います。

  4. For i = 1 To Len(myRng)

    • セル内の文字列を1文字ずつ調べます。 `Len(myRng)` はセル内の文字数を返します。

  5. myStr = Mid(myRng.Value, i, 2)

    • セル内の文字列から2文字ずつ取り出します。`i` は開始位置を示します。

  6. If myStr Like "社保" Then

    • 2文字が「社保」と一致するかをチェック します。

  7. With myRng.Characters(Start:=i, Length:=2).Font

    • 一致した場合、その文字のフォント色を変更 します。ここでは赤色(ColorIndex = 3)にします。

  8. Application.ScreenUpdating = True

    • 画面の更新を再開 します。

このプロシージャを使うことで、Excelの選択範囲内の「社保」という文字列だけを簡単に赤色に変更することができます。

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


Highlight "社保" in Red

This explanation is created with ChatGPT.

This procedure changes the text color to red if the characters "社保" (Shaho) are found in any cell within the selected range.

How It Works

  1. Application.ScreenUpdating = False

    • Stops screen updating to prevent flickering during the process.

  2. Dim myRng As Range, myStr As String, i As Integer

    • Declares variables. `myRng` for cell ranges, `myStr` for two-character strings within the cell, and `i` as a counter.

  3. For Each myRng In Selection

    • Iterates through each cell in the selected range.

  4. For i = 1 To Len(myRng)

    • Examines each character in the cell. `Len(myRng)` returns the number of characters in the cell.

  5. myStr = Mid(myRng.Value, i, 2)

    • Extracts two characters at a time from the cell's text. `i` is the starting position.

  6. If myStr Like "社保" Then

    • Checks if the two characters match "社保".

  7. With myRng.Characters(Start:=i, Length:=2).Font

    • If matched, changes the font color of those characters. Here, it changes to red (ColorIndex = 3).

  8. Application.ScreenUpdating = True

    • Resumes screen updating.

This procedure allows you to easily change the color of the text "社保" to red within any selected range in Excel.

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

キーワード
#excel #できること #vba #赤色変更 #文字列検索 #セル内文字列 #プロシージャ #初心者向け #プログラミング #Excelマクロ #文字列操作 #範囲選択 #色変更 #自動化 #スクリプト #コード解説 #マイクロソフト #エクセル #スプレッドシート #画面更新

#excel #capabilities #vba #redtext #textsearch #celltext #procedure #beginnerfriendly #programming #excelmacro #textmanipulation #rangeselection #colorchange #automation #script #codeexplanation #microsoft #excelspreadsheet #screenupdating

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