
時間帯だけを隣のセルにひとつずつだすよ
この説明は、ChatGPTで作成しています。
このマクロは、Excelのセルに書かれた文章の中から 時間帯(例: 10:30~12:00) を見つけ出し、それを 隣のセル に順番に書き出すプログラムです。
例えば、「10:30~12:00の予定」と書かれているセルがあれば、隣のセルに「10:30~12:00」と書き出します。もし「13:15~15:45と16:00~18:30」といったように 複数の時間帯が書かれていた場合は、隣のセルに一つずつ並べていきます。
マクロの動き
正規表現(パターンマッチング) を使って、 「〇〇:〇〇~〇〇:〇〇」 という時間帯の形式を探します。
「翌10:30~12:00」のような 「翌」 がついた時間も対象にできます。
「翌」が不要なら "(翌)?" の部分を削除するといいです。
選択したセルの テキストを読み取る。
見つかった時間帯を 隣のセルに順番に書き込む。
画面のチラつきを防ぐため に Application.ScreenUpdating = False を使い、最後に True に戻します。
ポイント
正規表現(RegExp) を使うことで、時間のフォーマットを簡単に抽出できます。
複数の時間帯がある場合 でも、自動で横に並べてくれます。
Selection を使っているので、 マクロを実行する前に範囲を選択 しておく必要があります。
使い方
時間が含まれているセルを選択
マクロを実行
隣のセルに時間が抽出される!
簡単に時間を整理したいときに便利です✨
関連リンク
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
'「翌」が必要ないときはPatternのなかにある「(翌)?」この部分を削除する
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 ' 隣の列から開始
' マッチした時間帯を順に出力
For i = 0 To Matches.count - 1
myRng.Offset(0, j).Value = Matches.item(i)
j = j + 1 ' 次の列へ
Next i
Next myRng
Application.ScreenUpdating = True
End Sub
関連キーワード
#excel #できること #vba #正規表現 #テキスト処理 #時間帯 #データ整理 #エクセル自動化 #プログラミング初心者 #業務効率化 #スケジュール管理 #文字列操作 #エクセル活用 #セル操作 #データ抽出 #時間管理 #VBAマクロ #エクセルマクロ #初心者向けVBA #Excel時間抽出
English Translation
Extracting Time Slots into Adjacent Cells
This explanation is created using ChatGPT.
This macro scans the selected Excel cells for time slots (e.g., 10:30~12:00) and writes them into the adjacent cells one by one.
For example, if a cell contains "10:30~12:00 meeting," the macro will extract "10:30~12:00" and place it in the adjacent cell. If a cell contains "13:15~15:45 and 16:00~18:30," the macro will extract both time slots and write them into separate adjacent cells.
How It Works
Uses Regular Expressions (RegExp) to find time slots in the format "HH:MM~HH:MM".
It can also detect "翌" (next day) if present, like "翌10:30~12:00".
If "翌" is unnecessary, remove "(翌)?" from the pattern.
Reads text from selected cells.
Writes the found time slots into adjacent cells.
Prevents screen flickering using Application.ScreenUpdating = False.
Key Points
Uses Regular Expressions (RegExp) to efficiently extract time formats.
Handles multiple time slots, writing them into adjacent cells.
Requires selecting the target cells before running the macro.
How to Use
Select the cells containing time-related text.
Run the macro.
The extracted times appear in adjacent cells!
Great for organizing schedules and improving efficiency.✨
Related Links
Related Keywords
#excel #whatispossible #vba #regex #textprocessing #timeslots #dataorganization #excelautomation #beginnerprogramming #workefficiency #schedulemanagement #stringmanipulation #excelusage #celloperations #dataextraction #timemanagement #vbamacro #excelmacro #beginnerVBA #extracttimeinexcel