group values based on time slot & week day

rameshppc

Board Regular
Joined
Jun 10, 2017
Messages
114
Office Version
  1. 2013
Platform
  1. Windows
Hi good after noon,

i have train numbers and concerned time for all week days in my input sheet. I want to extract the data based on time slot & week day mentioned in the output sheet format.
sample workbook. I have attached sample workbook for your kind reference. kindly help me get the result.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Kindly give a shot @rameshppc

You may double click Output 1 A1 to automatically run the code (whenever you doubleclick A1 (Output 1 sheet) the code will run)

Right click Output 1 -> View code -> Paste below code

SAMPLED WORK.xlsx
ABCDEFGHIJKL
1FRIDAY
2FROMTOFROMTOFROMTOFROMTOFROMTOFROMTO
37:307:598:008:298:308:599:009:299:309:5910:0010:29
4NUMBERTIMENUMBERTIMENUMBERTIMENUMBERTIMENUMBERTIMENUMBERTIME
5206447:58226668:07126808:32126269:421267710:02
6225047:52226168:222263810:12
7125168:27
8
OUTPUT 1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
K3Cell Valuecontains "DROP DOWN"textNO
K3Cell Valuecontains "DROP DOWN"textNO
Cells with Data Validation
CellAllowCriteria
A1:L1List=INPUT!$A$1:$N$1


VBA Code:
Option Compare Text
Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim ws As Worksheet
Dim ws2 As Worksheet
Dim a(), b(), o()
Dim f As Range
Dim i%, k%, j%
Dim chec As Byte
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
dict.Comparemode = vbTextCompare
Set ws = Sheets("Input")
Set ws2 = Sheets("Output 1")
ReDim b(1 To 5000, 1 To 12) 'reserve row 5000 , column 12 bcos A to L From to

If Target.Row = 1 Then

Set f = ws.Range("a1:r1").Find(ws2.[a1].Value, LookIn:=xlValues)

ws2.[a5:l5000].ClearContents 'clear output1 range a5 : l5000

a = ws.Range(ws.Cells(f.Row, f.Column), ws.Cells(ws.UsedRange.Rows.Count, f.Column + 1)).Value2
o = ws2.Range("a3:l3").Value

For i = 3 To UBound(a, 1)
    For j = 1 To UBound(o, 2) Step 2 'Loop through From To Time
        If a(i, 2) >= o(1, j) And a(i, 2) < o(1, j + 1) Then 'Check If Value >= From and lower to ) A3:L3
            If dict.exists(o(1, j)) Then 'If already exist, then add one row below for array
                k = k + 1
            Else
                dict.Add o(1, j), 1 'If 1st time found, then put in row 1
                k = 1
            End If
            b(k, j) = a(i, 1) 'Number
            b(k, j + 1) = Format(a(i, 2), "Short time") 'Time
            chec = 1 'If already found then skip from & time loop
            If chec = 1 Then Exit For
        End If
    Next j
Next i

ws2.[a5].Resize(UBound(b, 1), UBound(b, 2)).Value = b
End If
End Sub
 
Last edited:
Upvote 0
Adjust a little bit for the array of B, kindly use below code instead

VBA Code:
Option Compare Text
Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim ws As Worksheet
Dim ws2 As Worksheet
Dim a(), b(), o()
Dim f As Range
Dim i%, k%, j%, lrow%
Dim chec As Byte
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
dict.Comparemode = vbTextCompare
Set ws = Sheets("Input")
Set ws2 = Sheets("Output 1")
ReDim b(1 To 5000, 1 To 12) 'reserve row 5000 , column 12 bcos A to L From to

If Target.Row = 1 Then

Set f = ws.Range("a1:r1").Find(ws2.[a1].Value, LookIn:=xlValues)

ws2.[a5:l5000].ClearContents 'clear output1 range a5 : l5000
lrow = ws.Cells(Rows.Count, f.Column).End(xlUp).Row
a = ws.Range(ws.Cells(f.Row, f.Column), ws.Cells(lrow, f.Column + 1)).Value2
o = ws2.Range("a3:l3").Value

For i = 3 To UBound(a, 1)
    For j = 1 To UBound(o, 2) Step 2 'Loop through From To Time
        If a(i, 2) >= o(1, j) And a(i, 2) < o(1, j + 1) Then 'Check If Value >= From and lower to ) A3:L3
            If dict.exists(o(1, j)) Then 'If already exist, then add one row below for array
                dict(o(1, j)) = dict(o(1, j)) + 1
            Else
                dict.Add o(1, j), 1 'If 1st time found, then put in row 1
            End If
            b(dict(o(1, j)), j) = a(i, 1)   'Number
            b(dict(o(1, j)), j + 1) = Format(a(i, 2), "Short time") 'Time
            chec = 1 'If already found then skip from & time loop
            If chec = 1 Then Exit For
        End If
    Next j
Next i

ws2.[a5].Resize(UBound(b, 1), UBound(b, 2)).Value = b
End If
End Sub
 
Upvote 0
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: group values based on time slot & week day
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,215,412
Messages
6,124,761
Members
449,187
Latest member
hermansoa

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