見出し画像

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

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

このプロシージャは、選択したセル範囲内の文字列に対して、「駅」という漢字が含まれている場合、その漢字だけを赤色にするものです。

仕組みの説明

  1. Application.ScreenUpdating = False

    • 画面の更新を停止します。これにより、処理中の画面のちらつきを防ぎ、処理速度を向上させます。

  2. 変数の宣言

    • myRng: 選択したセル範囲を1つずつ表します。

    • myStr: セル内の文字を1文字ずつ保持します。

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

    • a: このプロシージャでは使われていませんが、長い整数型の変数です。

  3. For Each myRng In Selection

    • 選択したセル範囲内の各セルを1つずつ処理します。

  4. For i = 1 To Len(myRng)

    • 現在のセルの文字列を1文字ずつチェックします。

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

    • セル内の文字列のi番目の文字を取得します。

  6. If myStr Like "駅" Then

    • 取得した文字が「駅」であるかどうかをチェックします。

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

    • 「駅」であれば、その文字のフォントの色を赤色に設定します(ColorIndex = 3 は赤色を意味します)。

  8. Application.ScreenUpdating = True

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

このプロシージャは、簡単な操作で特定の漢字を強調表示することができます。

追加情報

  • 下記の記事を参考にしました。著者の方、ありがとうございました!

    • http://www.eurus.dti.ne.jp/~yoneyama/Excel/vba/vba_cell_font.html

      • この記事の優れた点を、説明の仕方や構成の観点から5つ挙げます:

        1. 具体的な目的を明確に示している:タイトルで「セル内の文字の一部だけ色やサイズを変える」という具体的な目標を提示し、読者に記事の内容を即座に理解させている。

        2. 複数のアプローチを提示:同じ目的を達成するための3つの異なるVBAコードを提供し、読者に選択肢を与えている。

        3. コードの構造化と注釈:各コードブロックが適切に構造化され、重要な部分には日本語のコメントが付けられており、理解しやすくなっている。

        4. 段階的な複雑さ:最初の例から順に複雑さが増していく構成になっており、読者が徐々に高度な技術を学べるようになっている。

        5. 実用的な例の使用:「数字だけをヒラギノ角ゴ W6」にするなど、実際の業務で使えそうな具体例を用いて説明しており、読者が自分の状況に適用しやすくなっている。

  • 詳細なVBAリファレンスについては、Excel VBA リファレンス | Microsoft Learn をご覧ください。

  • このプロシージャの解説動画はこちら:この記事のYouTube動画はこちら


Explanation in English

Color Only the Kanji "駅" in Red if It Exists

This explanation is created by ChatGPT.

This procedure changes the color of the kanji "駅" to red if it exists in the selected cell range.

Explanation of the Mechanism

  1. Application.ScreenUpdating = False

    • Stops the screen from updating. This prevents flickering and speeds up the process.

  2. Variable Declaration

    • myRng: Represents each cell in the selected range.

    • myStr: Holds each character in the cell.

    • i: Loop counter to check each character.

    • a: A long integer variable, not used in this procedure.

  3. For Each myRng In Selection

    • Processes each cell in the selected range one by one.

  4. For i = 1 To Len(myRng)

    • Checks each character in the current cell's string.

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

    • Gets the i-th character in the cell's string.

  6. If myStr Like "駅" Then

    • Checks if the obtained character is "駅".

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

    • If it is "駅", sets the font color of that character to red (ColorIndex = 3 means red).

  8. Application.ScreenUpdating = True

    • Finally, resumes screen updating.

This procedure allows you to highlight specific kanji with simple operations.

Additional Information

  • I referred to the following article. Thank you to the author!

    • http://www.eurus.dti.ne.jp/~yoneyama/Excel/vba/vba_cell_font.html

      • Here are the 5 notable points of the article, focusing on its presentation and structure rather than technical content:

        1. Clearly stated specific purpose: The title immediately conveys the concrete goal of "changing the color or size of only part of the text in a cell," allowing readers to quickly grasp the article's content.

        2. Multiple approaches presented: The article provides three different VBA code examples to achieve the same objective, giving readers options to choose from.

        3. Structured code with annotations: Each code block is properly structured and includes Japanese comments for important sections, making it easier to understand.

        4. Gradual increase in complexity: The examples are arranged in order of increasing complexity, allowing readers to progressively learn more advanced techniques.

        5. Use of practical examples: The article employs concrete examples that could be useful in actual work situations, such as "making only numbers Hiragino Kaku Gothic W6," making it easier for readers to apply the techniques to their own scenarios.

  • For detailed VBA references, see Excel VBA Reference | Microsoft Learn.

  • Watch the explanatory video of this procedure here: Watch this YouTube video here.


ハッシュタグ

#excel #できること #vba #漢字 #色変更 #フォント #セル #選択範囲 #ループ #画面更新 #プログラミング #条件分岐 #手順 #処理速度 #VBA入門 #色設定 #文字列操作 #セル操作 #テキスト処理 #プログラム

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