見出し画像

日付だけを赤字にするよ4パターン

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

このVBAプロシージャ「日付だけを赤字にするよ4パターン」は、Excelのセルに含まれているテキストの中から日付の部分だけを自動的に見つけ出し、その部分を赤色に変えるものです。日付の形式には、次の4つのパターンに対応しています。

  1. 「YYYY/MM/DD」(例:2023/09/09)

  2. 「YYYY-MM-DD」(例:2023-09-09)

  3. 「YYYY_MM_DD」(例:2023_09_09)

  4. 「YYYY年MM月DD日」(例:2023年9月9日)

仕組みのポイント

  1. セルの選択範囲をループ処理
    Excelで選択されたセルの範囲の中で、1つずつセルの内容をチェックします。それぞれのセルに対して、正規表現を使って日付を探し、その日付が見つかったら、その部分だけを赤色に変更します。

  2. 日付部分だけ赤字にする
    マッチした部分(つまり見つけた日付)を赤色に変えるためには、`Characters`というExcelの機能を使います。これは、セル全体ではなく、一部分だけの書式を変更するための方法です。

動作の流れ

  1. まず、画面の更新を一時停止して処理を高速化します(`Application.ScreenUpdating = False`)。

  2. 次に、正規表現の設定を行い、日付のパターンを指定します。

  3. 選択したセルの中から1つずつ内容を確認し、日付を見つけたら、その部分を赤くします。

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

このVBAを使うと、大量のテキストの中から日付を見つけて強調表示するのが簡単になります。

Sub 日付だけを赤字にするよ4パターン()
    Application.ScreenUpdating = False
    Dim reg As Object
    Set reg = CreateObject("VBScript.RegExp") 'オブジェクト作成
    Dim myRng As Range
    Dim txt As String
    Dim matches, match As Object
    With reg
        .Pattern = "\d{4}/\d{1,2}/\d{1,2}|\d{4}-\d{1,2}-\d{1,2}|\d{4}_\d{1,2}_\d{1,2}|\d{4}年\d{1,2}月\d{1,2}日"
        .IgnoreCase = True
        .Global = True
    End With

    For Each myRng In Selection
        txt = myRng.Value
        Set matches = reg.Execute(txt)
        
        If matches.count > 0 Then
            'マッチした文字列の部分だけ赤色にする
            For Each match In matches
                myRng.Characters(match.FirstIndex + 1, match.Length).Font.Color = vbRed
            Next match
        End If
    Next myRng

    Application.ScreenUpdating = True
End Sub


キーワード

#vba #excel #日付フォーマット #正規表現 #文字列操作 #書式設定 #フォントカラー #文字色変更 #部分的なフォント変更 #セル操作 #テキスト検索 #自動処理 #条件付き書式 #セル範囲 #エクセルマクロ #マッチング #プログラミング初心者 #できること #簡単操作 #日付検索


English Translation

Highlight Dates in Red (4 Patterns)

This explanation is created using ChatGPT.

The VBA procedure "Highlight Dates in Red (4 Patterns)" automatically detects the dates in the text within Excel cells and changes their color to red. It supports the following four date formats:

  1. "YYYY/MM/DD" (e.g., 2023/09/09)

  2. "YYYY-MM-DD" (e.g., 2023-09-09)

  3. "YYYY_MM_DD" (e.g., 2023_09_09)

  4. "YYYY年MM月DD日" (e.g., 2023年9月9日)

How It Works

  1. Looping Through Selected Cells
    The procedure processes each cell in the selected range one by one. It checks the text of each cell, searches for dates using the regular expression, and changes only the matched portions (the dates) to red.

  2. Changing Only Date Text to Red
    To highlight only the matched parts (the detected dates), the `Characters` method is used. This allows the VBA to modify the formatting of just a portion of the cell's content, not the entire cell.

Step-by-Step Process

  1. The screen update is temporarily turned off to speed up the process (`Application.ScreenUpdating = False`).

  2. Then, the regular expression is configured to detect date patterns.

  3. Each selected cell is checked for dates, and if a match is found, the date text is highlighted in red.

  4. Finally, screen updating is turned back on.

This VBA makes it easy to find and highlight dates in large blocks of text.



Keywords

#vba #excel #dateformats #regex #stringoperations #fontcolor #highlightingtext #partialformatting #celloperations #textsearch #automation #conditionalformatting #rangehandling #excelmacros #matching #beginnerprogramming #capabilities #easytouse #datesearch

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