final code 2
final code 2
Dim ws As Worksheet
Dim lastRow As Long
Dim colSegment As Range, colStart As Range, colEnd As Range
Dim cell As Range
' Find original columns for Segment, Start Time, End Time
Set colSegment = ws.Rows(1).Find("Segment", LookAt:=xlWhole)
Set colStart = ws.Rows(1).Find("Start Time", LookAt:=xlWhole)
Set colEnd = ws.Rows(1).Find("End Time", LookAt:=xlWhole)
' Insert new columns for Segment, Start Time, End Time at the beginning (shift
existing columns to the right)
ws.Columns("A:C").Insert Shift:=xlToRight
' Move the data from the original columns to the new columns (starting from row
2, avoid headers)
If Not colSegment Is Nothing Then
ws.Range(ws.Cells(2, colSegment.Column), ws.Cells(lastRow,
colSegment.Column)).Copy
ws.Cells(2, 1).PasteSpecial Paste:=xlPasteValues
End If
' Now remove the old "Segment", "Start Time", "End Time" columns to avoid
duplication
On Error Resume Next
If Not colSegment Is Nothing Then colSegment.EntireColumn.Delete
If Not colStart Is Nothing Then colStart.EntireColumn.Delete
If Not colEnd Is Nothing Then colEnd.EntireColumn.Delete
On Error GoTo 0
' Format Start Time and End Time to add space between numbers and AM/PM
For Each cell In ws.Range("B2:B" & lastRow)
If InStr(cell.Value, "am") > 0 Then
cell.Value = Replace(cell.Value, "am", " AM")
ElseIf InStr(cell.Value, "pm") > 0 Then
cell.Value = Replace(cell.Value, "pm", " PM")
End If
Next cell
' Apply coloring only to the Segment column based on specific criteria
For Each cell In ws.Range("A2:A" & lastRow)
Select Case True
' Gray color for "hidden" and "crew only" events in Segment
Case LCase(cell.Value) Like "hidden", LCase(cell.Value) Like "crew
only"
cell.Interior.Color = RGB(169, 169, 169) ' Gray