Is there a way to sort time according to a Broadcast Day (6am-6am)

kokoanutt

New Member
Joined
May 17, 2024
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Is there any way to create a Macro to sort a column with Hit Times according to a Broadcast Day of 6am-6am? I have searched everywhere and have not been able to find anything. The way that Excel currently sort information is from Smallest to Largest. However, I want something with a Hit Time of 06:30:00 to be listed before something with a Hit Time of 02:15:30, based on the Broadcast Day. Hopefully this makes sense to someone and there is genius out there that can assist. Thank You!
 
Excellent question on the display time @Akuini . I'm not sure who created this logic and why it is displayed in the 12-hour format. In other modules within this ecosystem, the time is displayed in the 24-hour format. Doesn't make any sense.

What does this statement mean?
"For i = 1 To UBound (va, 1)"
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Did you try my code? Does it work as expected?
What does this statement mean?
"For i = 1 To UBound (va, 1)"
Variable va holds values from column B, and we need to loop through those values in va and change them as needed.
"'For i = 1 To UBound(va, 1)" is used to loop through those values starting from the first value until the last one.
 
Upvote 0
Did you try my code? Does it work as expected?

Variable va holds values from column B, and we need to loop through those values in va and change them as needed.
"'For i = 1 To UBound(va, 1)" is used to loop through those values starting from the first value until the last one.
I did try the code, but it got stuck at "For i = 1 To UBound(va, 1)" :(, which was why I was asking. I was going to try and figure it out.
 
Upvote 0
In my actual Workbook
Using the code in post #10, if any grouped data consists of only 1 row, the code will fail.
So, try this one instead:
VBA Code:
Sub Sort_Hour_3()
Dim dt As Date
Dim i As Long, n As Long
Dim va
Dim c As Range, f As Range

Application.ScreenUpdating = False
'"A2" means start at A2
Set f = Range("A2", Cells(Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeConstants)

For Each c In f.Areas
    va = c.Offset(, 1) 'get col B
    If IsArray(va) Then
        For i = 1 To UBound(va, 1)
            dt = CDate(va(i, 1))
            dt = DateAdd("h", 18, dt)  'adding 18 hours
            va(i, 1) = Format(dt, "hh:nn:ss AM/PM")
        Next
    End If
    c.Offset(, 5) = va 'fill temporary helper column F with time+18 hours
    With c.Resize(, 6) 'expand to col A:F
        'sort ascending by col 1, then  sort ascending by col 6
        .Sort Key1:=.Columns(1), Order1:=xlAscending, Key2:=.Columns(6), Order2:=xlAscending, Header:=xlNo
        c.Offset(, 5).ClearContents
    End With
Next
   
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Using the code in post #10, if any grouped data consists of only 1 row, the code will fail.
So, try this one instead:
VBA Code:
Sub Sort_Hour_3()
Dim dt As Date
Dim i As Long, n As Long
Dim va
Dim c As Range, f As Range

Application.ScreenUpdating = False
'"A2" means start at A2
Set f = Range("A2", Cells(Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeConstants)

For Each c In f.Areas
    va = c.Offset(, 1) 'get col B
    If IsArray(va) Then
        For i = 1 To UBound(va, 1)
            dt = CDate(va(i, 1))
            dt = DateAdd("h", 18, dt)  'adding 18 hours
            va(i, 1) = Format(dt, "hh:nn:ss AM/PM")
        Next
    End If
    c.Offset(, 5) = va 'fill temporary helper column F with time+18 hours
    With c.Resize(, 6) 'expand to col A:F
        'sort ascending by col 1, then  sort ascending by col 6
        .Sort Key1:=.Columns(1), Order1:=xlAscending, Key2:=.Columns(6), Order2:=xlAscending, Header:=xlNo
        c.Offset(, 5).ClearContents
    End With
Next
  
Application.ScreenUpdating = True
End Sub
🥳 I cannot thank you enough!!!! 🙌
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,790
Members
451,589
Latest member
Harold14

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top