見出し画像

文章の先頭にチェック済みを赤字で追加するよ

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

このVBAコードは、選択されたセルの内容 の先頭に「チェック済_」という文字を追加し、その部分を赤字にするプログラムです。例えば、セルに「データ」という内容があった場合、「チェック済_データ」となり、先頭の「チェック済_」が赤色で表示されます。

プログラムの流れ

  1. 画面更新を停止
    `Application.ScreenUpdating = False` で、コードが実行される間に画面のチラつきを防ぎます。処理が完了するまで画面は更新されません。

  2. セルごとの処理
    `For Each myRng In Selection` というループで、選択範囲内のセル それぞれに対して処理を行います。

  3. 先頭に「チェック済_」を追加
    各セルの内容に「チェック済_」という文字列を追加します。例えば、セルの内容が「タスク」だった場合、そのセルの値は「チェック済_タスク」に変更されます。

  4. 赤字に変更
    `For i = 1 To Len(myRng)` というループで、各セルの文字列を1文字ずつ確認し、「チェック済_」の部分が見つかると、その部分を赤色にします。この色指定には `.ColorIndex = 3` を使用しています。Excelで「3」というのは赤色を意味します。

  5. エラー処理
    `On Error Resume Next` で、万が一エラーが発生した際にプログラムが停止しないようにしています。

  6. 画面更新を再開
    `Application.ScreenUpdating = True` で、処理が完了した後に画面更新を再開します。

実際に使うと

選択されたセルに対して、「チェック済_」という文字を自動的に追加し、その部分を赤字にしてくれる便利な機能です。例えば、タスク管理や進捗確認などで「チェック済み」の表示を強調したい場合に役立ちます。


Excel VBA リファレンス | Microsoft Learn
Excelアイコンは、Icons8が作成したものです
この記事のYouTube動画はこちら

Sub 文章の先頭にチェック済みを赤字で追加するよ()
    Application.ScreenUpdating = False
    On Error Resume Next
    Dim myRng As Range
    Dim myStr As String
    Dim i As Long
    For Each myRng In Selection
            myRng = "チェック済_" & myRng.Value
            For i = 1 To Len(myRng)
                myStr = Mid(myRng.Value, i, 6)
                '_済みがあったら書式変更
                If myStr Like "チェック済_" Then
                    With myRng.Characters(Start:=i, Length:=6).Font
                        .ColorIndex = 3
                    End With
                End If
            Next i
    Next myRng
    Application.ScreenUpdating = True
End Sub

キーワード

#excel #できること #vba #チェック済み #赤字 #セル操作 #文字列操作 #色変更 #タスク管理 #エラー処理 #選択範囲 #書式変更 #セル内文字操作 #自動化 #プログラミング #エクセルVBA #効率化 #初心者向け #進捗管理 #フォント操作


英語訳

Adding "Checked" in Red Text at the Beginning of Each Cell

This explanation is created using ChatGPT.

This VBA code adds the phrase "チェック済_" (which means "checked" in Japanese) at the beginning of each selected cell's content and changes the color of that text to red. For example, if a cell contains "Data," it will become "チェック済_Data," and the "チェック済_" part will be displayed in red.

Code Breakdown

  1. Stop Screen Updating
    `Application.ScreenUpdating = False` stops the screen from updating during the execution of the code, preventing flickering while the macro runs.

  2. Processing Each Cell
    The `For Each myRng In Selection` loop processes each cell in the selected range.

  3. Add "チェック済_" at the Beginning
    The content of each selected cell has "チェック済_" added to the beginning. For instance, if the cell originally contains "Task," it will now display "チェック済_Task."

  4. Change Text Color to Red
    A loop `For i = 1 To Len(myRng)` checks each character in the cell content. When it finds "チェック済_" at the beginning, it changes the color of that part of the text to red using `.ColorIndex = 3`, which represents red in Excel.

  5. Error Handling
    The line `On Error Resume Next` ensures that if any error occurs, the macro continues without stopping.

  6. Resume Screen Updating
    Once the code finishes, `Application.ScreenUpdating = True` resumes updating the screen.

Practical Use

This macro is useful when you want to automatically add a "checked" label at the beginning of selected cells and highlight it in red, making it ideal for managing tasks or progress checks.


Excel VBA Reference | Microsoft Learn
Excel icon by Icons8
YouTube Video for This Article


Keywords

#excel #vba #checked #redtext #celloperation #stringmanipulation #colorchange #taskmanagement #errorhandling #selectedrange #formatting #textinacell #automation #programming #excelvba #efficiency #beginners #progressmanagement #fontoperation

いいなと思ったら応援しよう!