見出し画像

文章の先頭に承認済みを赤字で追加するよ

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

このマクロは、選択したセルの内容の先頭に 「承認済」 を追加し、さらに「承認済」の文字を 赤色 に変更する仕組みです。具体的な動きとしては、以下のようになります。

  1. 画面の更新を一時的に停止して、動作がスムーズに進むようにしています。

  2. 次に、エラーが発生しても処理を続ける設定にしています。

  3. 選択しているセル を一つずつチェックして、セルの内容の前に 「承認済_」 を追加します。

  4. 追加した「承認済」の文字を探し出して、見つけた場合はその文字を 赤色 に設定します。

  5. すべてのセルに対して処理が終わると、最後に 画面の更新を再開 します。

このマクロを実行することで、たとえば「レポート」というセルの内容が「承認済_レポート」となり、「承認済_」の部分だけ赤く 表示されます。これにより、特定の情報がすでに承認されたことを視覚的に示すことができます。

以下のポイントでさらに詳しく見ていきましょう。

  • Application.ScreenUpdating = False / True

    • 処理の前後で画面の更新を停止・再開することで、マクロの実行速度を上げる工夫です。

  • On Error Resume Next

    • 万が一エラーが起きても、途中でマクロが止まらないようにしています。

  • 文字列の変更

    • 「承認済_」 をセルの先頭に追加して、後からこの文字列を探して色を付けています。

  • 色の変更

    • 「承認済_」 が見つかった場合、その文字だけを 赤色(色コード3)に変更します。

これを使えば、書類や報告書に対して承認が済んでいるものを一目で確認できるので、作業がスムーズに進みますね。

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, 4)
                '_済みがあったら書式変更
                If myStr Like "承認済_" Then
                    With myRng.Characters(Start:=i, Length:=4).Font
                        .ColorIndex = 3
                    End With
                End If
            Next i
    Next myRng
    Application.ScreenUpdating = True
End Sub

Adding "Approved" in Red at the Beginning of Text

This explanation was created by ChatGPT.

This macro is designed to add the text "承認済" (Approved) at the beginning of selected cell contents and change the color of the text to red. Here’s how it works:

  1. Temporarily stops screen updates to ensure smooth execution.

  2. Sets the macro to continue even if errors occur.

  3. Loops through each selected cell and adds "承認済_" at the beginning of the cell’s contents.

  4. Searches for the newly added text and changes it to red if found.

  5. Resumes screen updates once the process is complete.

For instance, if a cell originally contains "Report," it will be changed to "承認済_Report," with "承認済_" displayed in red, clearly indicating the item has been approved.

Let’s dive into the details:

  • Application.ScreenUpdating = False / True

    • Stops and resumes screen updates before and after processing, speeding up the macro.

  • On Error Resume Next

    • Ensures the macro continues even if an error occurs.

  • Modifying Text

    • Adds "承認済_" to the beginning of the cell’s content and later identifies this string to apply color formatting.

  • Color Formatting

    • If "承認済_" is found, the text is set to red (Color code 3).

By using this macro, you can easily identify approved documents or reports at a glance, making workflow smoother.


ハッシュタグ:
#excel #できること #vba #承認済み #マクロ #自動化 #効率化 #業務改善 #セル書式 #エラー処理 #画面更新 #選択範囲 #文字列追加 #色変更 #文字色 #承認管理 #ワークフロー #データ処理 #エクセル活用 #プログラミング初心者

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