時間帯の開始と終了を隣のセルにひとつずつだすよ
この説明は、ChatGPTで作成しています。
このマクロは、「〇時:〇分~〇時:〇分」 という形の時間が入った文字列から、開始時間と終了時間を取り出して、隣のセルにひとつずつ表示する プログラムです。
たとえば、
A1セルに「12:00~14:00」と書かれていたら、
このマクロを実行すると…
B1セルに「12:00」
C1セルに「14:00」
と表示されます✨
しかも、時間帯が1つのセルにいくつか入っていても、最大5組まで対応できます!
やっていることをカンタンに説明すると…
**正規表現(せいきひょうげん)**という、パターンを見つける仕組みを使って、
時間帯を見つけます。
→ ここでは「12:00~14:00」みたいな書き方のこと!見つけた時間帯を、「~」で分けて、
開始時間 と 終了時間 にします。それを、元のセルの右どなりのセルに順番に入れていきます。
→ 開始時間 → 終了時間 → 次の開始時間 → 次の終了時間…という感じ!
たとえばこんなデータに使えます♪
シフト表の時間管理(勤務時間が書いてある表)
イベントの開催時間が一覧になっている表
時間の並びを整理したいとき
スマホで見たときに、こういう情報が1つのセルにギュッと書かれてると見づらいですよね。
でもこのマクロを使えば、見やすく分けることができます♪
補足情報✨
*「翌」*という文字が含まれていてもちゃんと動きます。(例:翌2:00~翌6:00)
最大で 5組(10列分) まで取り出せます。
関連リンクはこちら👇
Sub 時間帯の開始と終了を隣のセルにひとつずつだすよ()
Application.ScreenUpdating = False
Dim reg As Object
Set reg = CreateObject("VBScript.RegExp")
Dim Matches As Object
Dim myRng As Range
Dim txt As String
Dim i As Long, j As Long
Dim splitTimes() As String
Const MAX_COLUMNS As Long = 10
With reg
.Pattern = "(翌)?\d{1,2}:(翌)?\d{1,2}~(翌)?\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)
j = 1 ' 隣の列から開始(Offset 0, 1)
For i = 0 To Matches.count - 1
If j + 1 > MAX_COLUMNS Then Exit For ' 最大10列(5ペア)まで
splitTimes = Split(Matches.item(i), "~")
If UBound(splitTimes) = 1 Then
myRng.Offset(0, j).Value = splitTimes(0)
myRng.Offset(0, j + 1).Value = splitTimes(1)
j = j + 2
End If
Next i
Next myRng
Application.ScreenUpdating = True
End Sub
ハッシュタグ(キーワード)
#excel #vba #できること #時間帯 #文字列処理 #開始時間 #終了時間 #時間抽出 #シフト表 #イベント管理 #隣のセルに出力 #正規表現 #RegExp #Split関数 #Offset #繰り返し処理 #If文 #最大列数制限 #セル操作 #見やすく整理
英語訳(English Translation)
Put Start and End Times in Adjacent Cells
This explanation is created using ChatGPT.
This macro extracts time ranges written like “12:00~14:00” from cells, and puts the start time and end time into the cells next to it.
For example, if cell A1 contains “12:00~14:00”,
after running this macro:
B1 will show “12:00”
C1 will show “14:00”
It can even handle up to 5 time pairs per cell!
What this macro does:
Uses regular expressions to detect time patterns like “12:00~14:00”.
Splits the time range into start time and end time.
Places them into the cells to the right of the original cell. → Start → End → Next Start → Next End...
Useful for:
Managing work shift schedules
Organizing event times
Making time data easier to view on mobile devices
Bonus Info:
Handles times with “翌” (meaning “next day” in Japanese), like “翌2:00~翌6:00”
Supports up to 5 pairs (10 columns) of time ranges
Helpful Links:
Hashtags
#excel #vba #thingsyoucando #timerange #textprocessing #starttime #endtime #timeextraction #shiftschedule #eventmanagement #outputnextcell #regex #RegExp #SplitFunction #Offset #loop #IfStatement #columnlimit #celloperation #organizedview
