見出し画像

バスという文字があったらそれだけ赤色にするよ

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

このプロシージャは、Excelのセルの中にある文字列をチェックし、*「バス」*という文字列があれば、その部分だけを赤色に変更するものです。以下に仕組みをわかりやすく説明します。

概要

  1. 画面更新を停止: `Application.ScreenUpdating = False` は処理が終わるまで画面の更新を止めることで、処理速度を速めます。

  2. 変数の宣言: 処理に必要な変数を宣言します。`myRng` はセル範囲、`myStr` は文字列、`i` と `a` はカウンタ変数です。

  3. セルのループ処理: `For Each myRng In Selection` で選択されているすべてのセルを順番に処理します。

  4. 文字列のチェック: `For i = 1 To Len(myRng)` でセル内の文字列を2文字ずつチェックします。

  5. 条件チェック: `If myStr Like "バス"` で文字列が「バス」と一致するかを確認します。

  6. 書式の変更: `With myRng.Characters(Start:=i, Length:=2).Font` で該当部分のフォント色を赤色に変更します。

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

コードの流れ

  1. Excelのセルを選択し、プロシージャを実行します。

  2. 選択されたセル内の文字列を2文字ずつ確認します。

  3. 文字列が「バス」に一致する場合、その部分だけの文字色を赤色に変更します。

このプロシージャを実行すると、選択したセル内の「バス」という文字列が赤色に変更されます。操作が簡単で、特定の文字列を強調したいときに便利です。


Bus Characters are Turned Red if Found

This explanation is created by ChatGPT.

This procedure checks the text in the cells of Excel and if it finds the string "Bus", it changes only that part to red. Here is a simple explanation of how it works.

Overview

  1. Stop Screen Updating: `Application.ScreenUpdating = False` stops screen updates until the process is finished to speed up processing.

  2. Variable Declaration: Declare the necessary variables for the process. `myRng` is the cell range, `myStr` is a string, `i` and `a` are counter variables.

  3. Loop Through Cells: `For Each myRng In Selection` processes all the selected cells one by one.

  4. Check Strings: `For i = 1 To Len(myRng)` checks the text in the cell 2 characters at a time.

  5. Condition Check: `If myStr Like "Bus"` checks if the text matches "Bus".

  6. Change Format: `With myRng.Characters(Start:=i, Length:=2).Font` changes the font color of the matching part to red.

  7. Resume Screen Updating: `Application.ScreenUpdating = True` resumes screen updates.

Code Flow

  1. Select cells in Excel and run the procedure.

  2. Check the text in the selected cells 2 characters at a time.

  3. If the text matches "Bus", change only that part to red.

When you run this procedure, the string "Bus" in the selected cells will be changed to red. It is simple to use and useful when you want to highlight specific text.


Keywords: #excel #vba #できること #プログラミング #初心者 #Excelマクロ #文字列検索 #セル操作 #書式設定 #テキストカラー #条件付き書式 #自動化 #選択範囲 #スクリプト #VBA解説 #コード例 #プログラム解説 #ビジネス活用 #Officeスキル #Excel自動化

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