見出し画像

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

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

このVBAプロシージャは、Excelシート上で選択されている範囲のセルから「研修」または「OJT」という文字列を探し、それを赤色に変更するものです。具体的には、セルの中の文字を一つ一つチェックし、対象の文字列が見つかったら、その部分の文字の色を赤色にします。

コードの仕組み

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

  2. 変数の宣言:
    `myRng`, `myStr`, `i`, `kwLength`, `kws`, `kw` という変数を使用します。`kws` には赤色に変更する対象の文字列(「研修」と「OJT」)が含まれます。

  3. 選択範囲のループ処理:
    `For Each myRng In Selection` で、選択された範囲内の各セルを処理します。

  4. 文字列の検索:
    各セル内の文字列を対象の文字列と比較し、一致する場合、その部分のフォントの色を赤色(`ColorIndex = 3`)に変更します。

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

コードのポイント

  • Mid関数:
    `Mid(myRng.Value, i, kwLength)` は、セル内の文字列から特定の位置にある部分文字列を取得します。

  • Like演算子:
    `If myStr Like kw` は、部分文字列が対象の文字列と一致するかどうかを確認します。

  • Charactersプロパティ:
    `myRng.Characters(Start:=i, Length:=kwLength).Font` で、セル内の特定の位置の文字のフォントを変更します。

このプロシージャは、選択範囲内のセルに対して文字列を検索し、その文字列を見つけると色を変更するため、視覚的にわかりやすくなります。


Excel VBA リファレンス | Microsoft Learn
https://learn.microsoft.com/ja-jp/office/vba/api/overview/excel
この記事のYouTube動画はこちら
https://youtu.be/ZGMqXhNLUnQ


English Translation

Sub Highlight "研修" and "OJT" in Red

This explanation is created with ChatGPT.

This VBA procedure scans the selected cells in an Excel sheet for the strings "研修" and "OJT" and changes their font color to red if found. Specifically, it examines each character in the cell and if it matches the target string, it changes the color of that part of the text to red.

How the Code Works

  1. Stop Screen Updating: `Application.ScreenUpdating = False` temporarily stops screen updates to speed up the process.

  2. Variable Declaration: Variables `myRng`, `myStr`, `i`, `kwLength`, `kws`, and `kw` are declared. `kws` contains the strings to be highlighted in red ("研修" and "OJT").

  3. Loop Through Selected Range: `For Each myRng In Selection` processes each cell in the selected range.

  4. Search Strings: The code compares the cell's text with the target strings, and if a match is found, changes that part's font color to red (`ColorIndex = 3`).

  5. Resume Screen Updating: Finally, `Application.ScreenUpdating = True` resumes screen updates.

Key Points of the Code

  • Mid Function: `Mid(myRng.Value, i, kwLength)` extracts a substring from the cell's text.

  • Like Operator: `If myStr Like kw` checks if the substring matches the target string.

  • Characters Property: `myRng.Characters(Start:=i, Length:=kwLength).Font` changes the font of specific characters in the cell.

This procedure makes it visually clear by highlighting the target strings in the selected cells.


Excel VBA Reference | Microsoft Learn
https://learn.microsoft.com/ja-jp/office/vba/api/overview/excel
Watch this YouTube video
https://youtu.be/ZGMqXhNLUnQ

キーワード
#excel #できること #vba #文字列検索 #文字色変更 #セル内文字列 #部分文字列取得 #フォント設定 #選択範囲処理 #文字列一致 #プロシージャ #プログラミング #スクリプト #自動化 #エクセル #業務効率化 #初心者向け #変数 #ループ処理 #Mid関数

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