![見出し画像](https://assets.st-note.com/production/uploads/images/145272034/rectangle_large_type_2_00f0d6c9c21726e2f4e95c29895df4b5.png?width=1200)
社会保険と社保という漢字があったらそれだけ赤色にするよ
この説明は、ChatGPTで作成しています。
このVBAプロシージャは、選択したセル範囲の中で「社保」や「社会保険」という文字列を見つけ、それを赤色に変更する機能があります。
1. 画面更新を停止する
まず、画面更新を停止して処理を高速化します。
Application.ScreenUpdating = False
2. 必要な変数を宣言する
次に、必要な変数を宣言し、検索対象の文字列を配列kws*に設定します。
Dim myRng As Range
Dim myStr As String
Dim i, kwLength As Integer
Dim kws, kw As Variant
kws = Array("社保", "社会保険") ' 赤色に変更する対象の文字列
3. 選択範囲の各セルを順番に処理する
選択範囲内の各セル(myRng)を順番に処理します。
For Each myRng In Selection
4. 各セルの文字列を調べる
各セルの文字列を対象の文字列数ごとに調べます。
For Each kw In kws
kwLength = Len(kw) ' 対象の文字列の長さを取得
For i = 1 To Len(myRng)
myStr = Mid(myRng.Value, i, kwLength)
5. 対象の文字列があったらフォントを赤くする
対象の文字列が見つかったら、その部分のフォントを赤色に変更します。
If myStr Like kw Then
With myRng.Characters(Start:=i, Length:=kwLength).Font
.ColorIndex = 3
End With
End If
Next i
Next kw
Next myRng
6. 画面更新を再開する
最後に、画面更新を再開してプロシージャを終了します。
Application.ScreenUpdating = True
このプロシージャを実行すると、選択したセル範囲内で「社保」や「社会保険」という文字列が赤色になります。
Excel VBA リファレンス | Microsoft Learn
キーワード:
#excel #できること #vba #社会保険 #社保 #文字列検索 #セル範囲 #文字色変更 #プロシージャ #マクロ #プログラミング #自動化 #業務効率化 #初心者向け #基本操作 #赤色変更 #選択範囲 #フォント変更 #配列操作 #テキスト操作
Procedure Explanation in English
This explanation is created using ChatGPT.
This VBA procedure searches for the terms "社保" and "社会保険" in the selected cell range and changes their font color to red.
1. Stop Screen Updating
First, stop screen updating to speed up the process.
Application.ScreenUpdating = False
2. Declare Necessary Variables
Next, declare necessary variables and set the search terms in the array kws.
Dim myRng As Range
Dim myStr As String
Dim i, kwLength As Integer
Dim kws, kw As Variant
kws = Array("社保", "社会保険") ' 赤色に変更する対象の文字列
3. Process Each Cell in the Selection
Process each cell in the selected range (myRng).
For Each myRng In Selection
4. Check Each Cell's Text
Check each character in the cell for the search terms.
For Each kw In kws
kwLength = Len(kw) ' 対象の文字列の長さを取得
For i = 1 To Len(myRng)
myStr = Mid(myRng.Value, i, kwLength)
5. Change Font Color if Match is Found
If a match is found, change the font color of that part to red.
If myStr Like kw Then
With myRng.Characters(Start:=i, Length:=kwLength).Font
.ColorIndex = 3
End With
End If
Next i
Next kw
Next myRng
6. Resume Screen Updating
Finally, resume screen updating to finish the procedure.
Application.ScreenUpdating = True
Running this procedure will change the color of "社保" and "社会保険" in the selected cells to red.
Excel VBA Reference | Microsoft Learn
YouTube Video for This Article
Keywords:
#excel #capabilities #vba #socialinsurance #shaho #stringsearch #cellrange #textcolorchange #procedure #macro #programming #automation #workflowefficiency #forbeginners #basicoperations #redcolorchange #selectionrange #fontchange #arrayoperations #textoperations