Search code examples
excelvbauniqueexcel-2007array-formulas

Extract time slot based on interval value


In cell A1 I have an interval value which is an integer which can either be 1, 5 or 15 which means 1 minute, 5 minutes or 15 minutes

In range "B1:B12", I have the sample data (it will be more)

12:03
12:03
12:06
12:06
12:09
12:11
12:14
12:15
12:15
12:16
12:31
12:32

Now in column C, I need to extract time slots based on number is available in cell A1.

If cell A1 has 15 then the column C would contain below

12:15
12:30

because first 15th minute in column starts at 12:15 then increment it by 15 mins until last available data which falls in that 15 min range.

If cell A1 has 5 then the column C would contain below

12:05
12:10
12:15
12:30

because first 5th minute starts at 12:05 then increment it by 5 mins until last available data which falls in that 15 min range.

and if cell A1 has 1 then extract everything except duplicates.

I hope I have explained the scenario properly. I am not able to think of a logic to do this in excel vba and need help with how to start so that I can try to apply the logic and comeup with a code.

Thank you

EDIT

I am adding a pic of the desired result. if A1 contains 1 then copy all timestamps in column C without the duplicates

if A1 contains 5 then show only 5 times of 5 min interval

if A1 contains 15 then show only 3 times of 15 min interval

enter image description here


Solution

  • Using VBA (1) returning an array

    You will need to ctrl-shif-enter

    Option Explicit
    
    Function get_unique_time_interval(ByVal interval_size As Double, ByVal input_data As Range) As Variant
    
      interval_size = interval_size / 60# / 24#
      
      Dim d
      Set d = CreateObject("Scripting.Dictionary")
      
      Dim r As Range
      For Each r In input_data
        Dim t As Double
        t = Int(r.Value / interval_size) * interval_size
        If Not d.exists(t) Then d.Add t, t
      Next r
      
      get_unique_time_interval = Application.WorksheetFunction.Transpose(d.keys)
    
    End Function
    

    (2) hard-coded outputting to C1

    option explicit
    Sub get_unique_time_interval_sub()
    
      Dim interval_size As Double: interval_size = ActiveSheet.Range("a1").Value
      Dim input_data As Range: Set input_data = Range(ThisWorkbook.Sheets(1).Range("b1"), ThisWorkbook.Sheets(1).Range("b1").End(xlDown))
      
      interval_size = interval_size / 60# / 24#
      
      Dim d
      Set d = CreateObject("Scripting.Dictionary")
      
      Dim r As Range
      For Each r In input_data
        Dim t As Double
        t = Int(r.Value / interval_size) * interval_size
        If Not d.exists(t) Then d.Add t, t
      Next r
      
      Dim v As Variant
      Dim i As Long
      i = 0
      If Not IsEmpty(Range("c1").Value) Then Range(Range("c1"), Range("c1").End(xlDown)).ClearContents
      For Each v In d.keys
        ThisWorkbook.Sheets(1).Range("c1").Offset(i, 0).Value = v
        i = i + 1
      Next v
    
    End Sub