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

曜日だけを赤字にするよ

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

    このVBAマクロは、Excelの選択範囲内にある曜日の文字列を見つけ出し、それらを赤色に変更するためのものです。以下、マクロの仕組みをわかりやすく説明しますね。

    1. Application.ScreenUpdating = False

    • これは、処理中に画面の更新を一時停止して、動作を速くするためのものです。

    2. 変数の宣言

    • myRng: セル範囲を格納するための変数です。

    • myStr: 各セルの文字列の一部を取り出すための変数です。

    • i と a: 繰り返し処理に使うカウンタ変数です。

    3. For Each myRng In Selection

    • ここで、選択したセル範囲内の各セルを1つずつ処理していきます。

    4. For i = 1 To Len(myRng)

    • この部分では、セル内の文字列の長さに応じて、その文字列を一文字ずつチェックします。

    5. myStr = Mid(myRng.Value, i, 3)

    • Mid関数を使って、セル内の文字列から指定した位置(i)から3文字を抜き出します。例えば、「月曜日」という文字列があれば、「月曜」を取り出します。

    6. If myStr Like "[月|火|水|木|金|土|日]曜日" Then

    • 取り出した文字列が「月曜日」や「火曜日」のような形に一致するかを確認します。

    7. With myRng.Characters(Start:=i, Length:=3).Font

    • 一致した場合、その3文字のフォントの色を赤色(ColorIndex = 3)に変更します。

    8. Application.ScreenUpdating = True

    • 最後に、画面の更新を再開します。

    このマクロを実行すると、選択したセルの中にある「月曜日」や「火曜日」などの曜日が赤字になります。例えば、セルに「今日は火曜日です」と入力されている場合、「火曜日」の部分だけが赤色に変わります。


    Sub 曜日だけを赤字にするよ()
        Application.ScreenUpdating = False
        Dim myRng As Range
        Dim myStr As String
        Dim i As Integer
        Dim a As Long
        On Error Resume Next
        For Each myRng In Selection
            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 #マクロ #曜日変更 #文字色 #Excel操作 #初心者向けVBA #カラーチェンジ #条件付き書式 #セル操作 #プログラミング初心者 #業務効率化 #ExcelVBA活用 #フォント変更 #色指定 #画面更新 #エクセル #曜日検索 #VBA解説


    英語翻訳

    Color Only Weekdays in Red

    This explanation was created with ChatGPT.

    This VBA macro is designed to search for day-of-the-week strings within the selected range in Excel and change their font color to red. Here's an easy-to-understand breakdown of how this macro works.

    1. Application.ScreenUpdating = False

    • This pauses screen updating during the process to make it run faster.

    2. Variable Declaration

    • myRng: Stores the cell range to be processed.

    • myStr: Stores portions of the string extracted from each cell.

    • i and a: Counter variables used in loops.

    3. For Each myRng In Selection

    • This loop processes each cell within the selected range one by one.

    4. For i = 1 To Len(myRng)

    • Here, the macro checks each character in the cell's string based on its length.

    5. myStr = Mid(myRng.Value, i, 3)

    • The Mid function extracts 3 characters from the string starting at position i. For example, if the string is "Monday," it extracts "Mon."

    6. If myStr Like "[月|火|水|木|金|土|日]曜日" Then

    • The macro checks if the extracted string matches a pattern like "Monday" or "Tuesday."

    7. With myRng.Characters(Start:=i, Length:=3).Font

    • If there's a match, the macro changes the font color of those 3 characters to red (ColorIndex = 3).

    8. Application.ScreenUpdating = True

    • Finally, it resumes screen updating.

    When you run this macro, any "Monday" or "Tuesday," etc., within the selected cells will turn red. For instance, if a cell contains "Today is Tuesday," only "Tuesday" will change to red.



    #excel #whatyoucando #vba #macro #weekdaychange #fontcolor #Exceloperations #VBAforbeginners #colorchange #conditionalformatting #celloperations #programmingbeginners #workefficiency #ExcelVBAusage #fontchange #colorassignment #screenupdating #excel #weekdaysearch #VBAexplanation

    あなたへのおすすめ