見出し画像

年月日をスラッシュにする

この説明文は、Claudeで作成しました。

Sub 年月日をスラッシュにするよ()

このプロシージャでは、セルに入力された年月日の形式を変換します。具体的には、"2023年6月17日"のような形式の文字列を"2023/6/17"という形式に変換します。

    Application.ScreenUpdating = False

この行では、Excelの画面更新を一時的に無効にすることで、処理を高速化します。

    Dim reg
    Set reg = CreateObject("VBScript.RegExp")   'オブジェクト作成

ここでは、正規表現を使用するためのオブジェクトを作成しています。正規表現は、特定のパターンに一致する文字列を検索したり置換したりするために使われます。

    Dim myRng As Range
    Dim txt As String
    Dim i As Long

変数を宣言しています。myRngはRangeオブジェクトで、txtは文字列、iは長整数型の変数です。

    With reg
        .Pattern = "(\d{4})年(\d{1,2})月(\d{1,2})日"
        .IgnoreCase = True
        .Global = True
    End With

この部分で、正規表現のパターンを設定しています。(\d{4})は4桁の数字、(\d{1,2})は1桁または2桁の数字にマッチします。よって、"2023年6月17日"のような形式の文字列がこのパターンにマッチします。.IgnoreCaseでは大文字小文字を区別しないよう設定し、.Globalでは文字列内のすべての一致するパターンを置換するよう指定しています。

    On Error Resume Next
    For Each myRng In Selection
            txt = myRng.Value
            txt = reg.Replace(txt, "$1/$2/$3")
            txt = LTrim(txt)
            myRng.Value = txt
    Next myRng

ここがメインの処理部分です。選択範囲のセルをmyRngとしてループし、それぞれのセルの値txtを取得します。reg.Replaceメソッドで、txtの中で正規表現にマッチする部分を置換します。$1は(\d{4})にマッチした部分(年)、$2は(\d{1,2})にマッチした部分(月)、$3は(\d{1,2})にマッチした部分(日)です。これらをスラッシュで区切ることで、年月日の形式を変換しています。LTrim関数で先頭の空白を削除し、変換後の値をmyRng.Valueに代入して元のセルを上書きします。

    Application.ScreenUpdating = True

最後に、Excelの画面更新を有効に戻します。

キーワード:
#正規表現 #パターン #置換 #文字列
#ループ #選択範囲 #セル #値
#オブジェクト #変数 #画面更新 #高速化

この動画へのリンク


Title: Change Year/Month/Day to Slash Format

This explanation was created by Claude.

Sub ConvertYearMonthDayToSlash()

This procedure converts the format of the year, month, and day entered in cells. Specifically, it converts a string in the format "2023年6月17日" (2023 year 6 month 17 day) to the format "2023/6/17".

    Application.ScreenUpdating = False

This line temporarily disables screen updating in Excel to speed up the processing.

    Dim reg
    Set reg = CreateObject("VBScript.RegExp")   'Create object

Here, an object is created to use regular expressions. Regular expressions are used to search for and replace strings that match specific patterns.

    Dim myRng As Range
    Dim txt As String
    Dim i As Long

Variables are declared here. myRng is a Range object, txt is a string, and i is a long integer variable.

    With reg
        .Pattern = "(\d{4})年(\d{1,2})月(\d{1,2})日"
        .IgnoreCase = True
        .Global = True
    End With

In this part, the regular expression pattern is set. (\d{4}) matches 4 digits, and (\d{1,2}) matches 1 or 2 digits. Therefore, this pattern matches strings in the format "2023年6月17日". .IgnoreCase sets the pattern to be case-insensitive, and .Global specifies that all matches in the string should be replaced.

    On Error Resume Next
    For Each myRng In Selection
            txt = myRng.Value
            txt = reg.Replace(txt, "$1/$2/$3")
            txt = LTrim(txt)
            myRng.Value = txt
    Next myRng

This is the main processing part. It loops through the cells in the selected range as myRng, and gets the value of each cell as txt. The reg.Replace method replaces the parts of txt that match the regular expression. $1 is the part that matches (\d{4}) (year), $2 is the part that matches (\d{1,2}) (month), and $3 is the part that matches (\d{1,2}) (day). These are separated by slashes to convert the year/month/day format. The LTrim function removes leading spaces, and the converted value is assigned back to myRng.Value, overwriting the original cell.

    Application.ScreenUpdating = True

Finally, screen updating in Excel is re-enabled.

Keywords:
#regular #expression #pattern #replace #string
#loop #selected #range #cell #value
#object #variable #screen #updating #speedup

This link

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