見出し画像

所要時間の最小と最大値を隣列にだすChatGPTと一緒につくったやつ

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

このプロシージャは、Excelのシートにある時間範囲(たとえば「09:00~17:30」のようなデータ)を認識し、それぞれの所要時間(開始時間から終了時間までの長さ)を計算します。そして、その計算結果の中から最小と最大の所要時間を、元のデータがあるセルの隣に表示するものです。

プロシージャの仕組み

  1. オブジェクトの設定
    プロシージャの最初に、正規表現(RegExp)を扱うためのVBScript.RegExpオブジェクトを作成しています。正規表現は、特定のパターンに一致する文字列を探し出すための強力なツールです。

  2. 正規表現の設定
    このコードでは、「09:00~17:30」や「13:15~24:00」といった時間形式を見つけるためのパターンを設定しています。具体的には、「24時間形式の時間」から「~」で区切られた時間ペアを探します。

  3. データを検出し、所要時間を計算
    Selectionにある各セルを一つずつ調べ、そのセル内に時間範囲が書かれていれば、正規表現を使ってマッチする部分を抽出します。その後、マッチした時間ペアの開始時間と終了時間を計算し、その間の所要時間を求めます。もし終了時間が「24:00」だった場合は、24時間扱いにして計算しています。

  4. 最小値と最大値を隣のセルに表示
    所要時間を計算した後、最小の所要時間と最大の所要時間を取り出し、それを元のセルの隣のセルに「最小~最大」の形式で出力します。時間は「0.00」形式(例えば「7.50」は7時間30分)で表示されます。

使い方

このマクロを実行するには、Excelで該当する時間データのあるセル範囲を選択し、マクロを実行します。すると、隣の列に自動的に最小と最大の所要時間が表示されます。


Sub 所要時間の最小と最大値を隣列にだすChatGPTと一緒につくったやつ()
    Application.ScreenUpdating = False
    Dim reg As Object
    Set reg = CreateObject("VBScript.RegExp")   'オブジェクト作成
    Dim myRng As Range
    Dim txt, totalDuration As String
    Dim matches, match As Object
    Dim startTime, endTime As Date
    Dim duration, maxDuration, minDuration As Double
        
    ' 正規表現パターンの設定
    With reg
        .Pattern = "\b([01]?[0-9]|2[0-3]):[0-5][0-9]~((24:00)|([01]?[0-9]|2[0-3]):[0-5][0-9])\b"
        .IgnoreCase = True
        .Global = True
    End With

    On Error Resume Next
    ' セル範囲のループ処理
    For Each myRng In Selection
        txt = myRng.Value
        totalDuration = "" ' 初期化
        maxDuration = 0 ' 最大所要時間を初期化
        minDuration = 1 ' 最小所要時間を24時間に初期化
        If reg.Test(txt) Then
            Set matches = reg.Execute(txt)
            
            ' マッチした場合
            For Each match In matches
                ' 時間帯の解析
                Dim times() As String
                times = Split(match.Value, "~")
                
                ' 開始時間と終了時間を変数に格納
                startTime = TimeValue(times(0))
                
                ' 終了時間が「24:00」の場合の処理
                If times(1) = "24:00" Then
                    endTime = TimeValue("00:00") + 1 ' 24時間を表す
                Else
                    endTime = TimeValue(times(1))
                End If
                
                ' 所要時間を計算
                duration = endTime - startTime
                
                ' 所要時間が負の場合、24時間経過とみなす
                If duration < 0 Then
                    duration = duration + 1 ' 124時間を表す
                End If
                
                ' 最大所要時間を更新
                If duration > maxDuration Then
                    maxDuration = duration
                End If
                
                ' 最小所要時間を更新
                If duration < minDuration Then
                    minDuration = duration
                End If
            Next match
            
            ' 最小値~最大値の所要時間を隣列に出力する(単位を時間に変換して出力)
            myRng.Offset(0, 1).Value = Format(minDuration * 24, "0.00") & "~" & Format(maxDuration * 24, "0.00")
        End If
    Next myRng
    
    Application.ScreenUpdating = True
End Sub

ハッシュタグ

#excel #できること #vba #所要時間 #正規表現 #時間計算 #マクロ #Excel操作 #時間範囲 #最大時間 #最小時間 #時間差 #24時間表記 #時間処理 #範囲選択 #スプレッドシート #自動化 #ビジネス効率化 #Excel関数 #初心者向け


English Translation

Procedure Title: Display the Minimum and Maximum Required Time in Adjacent Columns

This explanation is created using ChatGPT.

This procedure in Excel scans cells for time ranges (such as "09:00~17:30"), calculates the required time (duration between the start and end times), and then displays the minimum and maximum required times in the adjacent cells.

How It Works:

  1. Object Creation
    The procedure starts by creating a VBScript.RegExp object to handle regular expressions, which are used to identify specific patterns in the text (here, time ranges).

  2. Pattern Setup
    A pattern is defined to find time ranges like "09:00~17:30" or "13:15~24:00." The regular expression specifically looks for pairs of times in 24-hour format separated by "~."

  3. Detecting Data and Calculating Duration
    The code checks each selected cell and applies the regular expression to identify matching time ranges. Once found, it extracts the start and end times and calculates the duration between them. If the end time is "24:00," it treats it as a full 24-hour period.

  4. Displaying Min/Max in Adjacent Cells
    After calculating the durations, the procedure identifies the minimum and maximum durations and outputs them in the adjacent cells. The time is formatted as "0.00" (e.g., "7.50" means 7 hours and 30 minutes).

How to Use It:

Select a range of cells containing time data, then run the macro. It will automatically display the minimum and maximum required times in the adjacent column.



Hashtags

#excel #whatispossible #vba #requiredtime #regex #timecalculation #macro #exceloperations #timeranges #maxtime #mintime #timedifference #24hourformat #timeprocessing #rangeselection #spreadsheet #automation #businessefficiency #excelfunctions #beginners

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