見出し画像

時間帯データから最大所要時間を自動算出するChatGPTと一緒に作ったマクロ

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

このVBAマクロは、指定したセル範囲内にある*「時間帯データ」を解析し、各セルの中で最大の所要時間*を隣のセルに自動で表示するプログラムです。例えば、「10:30~13:45」や「22:00~24:00」といった形式のデータがある場合、このマクロは各セルの時間差を計算し、その中から最大の所要時間を抽出します。

マクロの流れ

  1. 初期設定と準備

    • ScreenUpdatingをFalseにして、画面のちらつきを防ぎます。

    • 正規表現(VBScript.RegExp)を使用して「時刻範囲」を認識する設定を行います。

    • パターンとして「00:00~23:59」のようなフォーマットに対応します。

  2. セル範囲をループして解析

    • 指定した範囲(選択範囲)内の各セルについて処理を行います。

    • 各セルのテキストを取得し、正規表現にマッチする部分(「時間帯」)を探します。

  3. 時間計算の実行

    • 各時間帯データについて、開始時間と終了時間を取得します。

    • 終了時間が「24:00」だった場合は、翌日の0時として扱います(24時間表現)。

    • 所要時間がマイナスになった場合、24時間を経過したとみなして補正します。

  4. 最大所要時間を判定

    • 各セル内の複数の時間帯から最大の所要時間を算出し、隣のセルに表示します(時間表示は「h:mm」形式)。

コード実行後の結果

指定セル範囲内の各セルに含まれる最大所要時間が、隣のセルに「h:mm」形式で表示されます。


Excel VBA リファレンス | Microsoft Learn
Excelアイコンは、Icons8が作成したものです
この記事のYouTube動画はこちら

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 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 ' 最大所要時間を初期化
        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 ' 1は24時間を表す
                End If
                
                ' 最大所要時間を更新
                If duration > maxDuration Then
                    maxDuration = duration
                End If
            Next match
            
            ' 最大所要時間を隣列に出力する(単位を時間に変換して出力)
            myRng.Offset(0, 1).Value = Format(maxDuration, "h:mm")
        End If
    Next myRng
    
    Application.ScreenUpdating = True
End Sub

ハッシュタグ

#excel #できること #vba #時間帯データ #所要時間計算 #正規表現 #時間管理 #最大時間計算 #セル自動入力 #VBAマクロ #業務効率化 #自動化ツール #VBScript #セル解析 #24時間表示 #プログラミング初心者 #エクセルマクロ #時間範囲抽出 #時間計算 #データ解析


Translation

Macro to Automatically Calculate Maximum Duration from Time Zone Data with ChatGPT

This explanation is created by ChatGPT.

This VBA macro is designed to analyze the "time zone data" within a specified cell range and automatically display the maximum required time from each cell in the adjacent cell. For example, if there are data such as "10:30~13:45" or "22:00~24:00," this macro calculates the time difference in each cell and extracts the maximum duration.

Macro Workflow

  1. Initialization and Setup:

    • ScreenUpdating is set to False to prevent screen flickering.

    • Regular expressions (VBScript.RegExp) are used to recognize "time ranges."

    • The pattern is set to match formats like "00:00~23:59."

  2. Loop through Cell Range for Parsing:

    • Each cell in the selected range is processed.

    • The text in each cell is retrieved, and parts that match the regular expression (time ranges) are searched.

  3. Execution of Time Calculation:

    • For each time range, start time and end time are obtained.

    • If the end time is "24:00," it is treated as 0:00 the next day (24-hour format).

    • If the duration is negative, 24 hours are considered elapsed, and the time is adjusted.

  4. Determining Maximum Required Time:

    • The maximum required time is calculated from multiple time ranges in each cell and displayed in the adjacent cell (in "h:mm" format).

Result after Code Execution

The maximum required time in each specified cell range will be displayed in "h:mm" format in the adjacent cell.


Hash Tags

#excel #capabilities #vba #timezonadata #timerequired #regex #timemanagement #maximumtime #cellautofill #vbamacro #efficiency #automationtools #VBScript #cellparsing #24hourdisplay #beginnerprogramming #excelmacro #timeextraction #timecalculation #dataanalysis

いいなと思ったら応援しよう!