メインコンテンツへスキップ
見出し画像

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

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

    このVBAマクロは、Excelのセルに入力されている文字列の先頭に「済み_」という文字を追加し、その部分を赤色にするためのものです。たとえば、セルに「作業中」という文字が入っていると、このマクロを実行すると「済み_作業中」に変わり、「済み_」の部分が赤字になります。

    動きの流れ

    1. 画面更新を停止:
      `Application.ScreenUpdating = False`でマクロの処理中に画面がチカチカしないように、一時的に画面の更新を停止しています。

    2. 選択範囲の取得:
      `For Each myRng In Selection`で、選択しているセル範囲を1つずつ処理していきます。今、選択しているセルが対象になります。

    3. 「済み_」を追加:
      `myRng = "済み_" & myRng.Value`で、それぞれのセルの先頭に「済み_」を追加しています。

    4. 赤字にする部分を探す:
      `For i = 1 To Len(myRng)`でセルの中の文字を1文字ずつ確認し、「済み_」があった場合に赤字にする準備をしています。

    5. 「済み_」を赤字に変更:
      `If myStr Like "済み_" Then`の部分で「済み_」という文字列を見つけたら、その3文字の色を赤色に変更しています。色を指定するために`.ColorIndex = 3`を使っています。この「3」が赤色を表します。

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

    注意点

    • このマクロは、現在選択しているセル範囲に対してのみ動作します。

    • すでに「済み_」が付いている文字列には再度「済み_」を追加してしまうので注意が必要です。


    参考リンク

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

    キーワード

    #excel #できること #vba #文字列操作 #色変更 #セル操作 #マクロ #先頭文字列追加 #テキスト加工 #セル範囲 #エクセル自動化 #プロシージャ #条件付き書式 #カラーインデックス #画面更新 #エラーハンドリング #データ処理 #フォント変更 #ユーザーフレンドリー #初心者向け


    Add "済み" in Red at the Beginning of the Text

    This explanation was created with ChatGPT.

    This VBA macro is designed to add the text "済み_" to the beginning of each cell's value within the selected range in Excel and change this added text to red. For example, if a cell contains "In Progress," running this macro will turn it into "済み_In Progress," with the "済み_" part colored in red.

    How it works

    1. Disable Screen Updating:
      `Application.ScreenUpdating = False` stops screen updates to prevent flickering while the macro runs.

    2. Process Each Cell in Selection:
      `For Each myRng In Selection` loops through each cell in the currently selected range.

    3. Add "済み_" to the Start:
      `myRng = "済み_" & myRng.Value` adds "済み_" to the start of each cell's text.

    4. Identify Text to Color Red:
      `For i = 1 To Len(myRng)` loops through each character in the cell, looking for the "済み_" string.

    5. Change Text Color to Red:
      When it finds "済み_", `If myStr Like "済み_" Then` changes those 3 characters to red using `.ColorIndex = 3` (where 3 represents red).

    6. Re-enable Screen Updating:
      Finally, `Application.ScreenUpdating = True` turns screen updating back on.

    Important Points

    • This macro operates only on the currently selected cells.

    • Be cautious if "済み_" is already present, as it will add it again.

    Reference Links

    Keywords

    #excel #howto #vba #textprocessing #changecolor #celloperation #macro #prefixaddition #textmanipulation #cellrange #excelautomation #procedure #conditionalformatting #colorindex #screenupdating #errorhandling #dataprocessing #fontchange #userfriendly #beginnerfriendly

    あなたへのおすすめ