Cell below value for a certain duration

garneson

New Member
Joined
Apr 27, 2020
Messages
4
Office Version
  1. 2010
Platform
  1. MacOS
Let’s say in column A I have a thousand rows of dates/times. Then in column B I have corresponding number data. I’m trying to understand how many times the data in column B is lower than a certain value (let’s say 5) for a given duration (let’s say 2 hours). In other words, look at the data in column B, and when the value is below 5, start duration counter from column A. Continue down column B until value jumps above 5. If duration is greater than 2 hours, count as 1 event. Sum all events.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hello garneson,

You will need to give us something more to go on, like a sample set and a manual solution that guides us how you want the calculation to proceed.
e.g. The duration of 2 hours is this the difference between each time value in column A compared to the previous row? Or is the value in A a duration? Or the duration is the difference between the timestamps where you'd have a value below 5 in column B?

I'm afraid the requirements are to vague for any of us to help you. Sample data may clarify all or provoke the correct questions being asked.
Thx.
 
Upvote 0
Welcome to the MrExcel board!

Sum all events.
Do you mean count all events?

If so, see if this user-defined functions does what you want. To implement ..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below (can use the icon at the top right of the code pane below) into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot below.
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)

VBA Code:
Function CountBelow(rngData As Range, lngLimit As Long, lngHours As Long) As Long
  Dim a As Variant
  Dim i As Long
  Dim dteStart As Date
  Dim blnStarted As Boolean
  
  a = rngData.Value
  For i = 1 To UBound(a)
    If a(i, 2) < lngLimit Then
      If Not blnStarted Then
        blnStarted = True
        dteStart = a(i, 1)
      End If
    Else
      If blnStarted Then
        If a(i - 1, 1) - dteStart >= lngHours / 24 Then CountBelow = CountBelow + 1
      End If
      blnStarted = False
    End If
  Next i
  If blnStarted Then
    If a(i - 1, 1) - dteStart >= 1 / 12 Then CountBelow = CountBelow + 1
  End If
End Function

Here is my sample data and results. I have manually coloured the cells to show the 3 areas that the function has counted.

garneson 2020-04-28 1.xlsm
ABCD
1Date/TimeValueCount
225-Apr-20 00:0053
325-Apr-20 00:063
425-Apr-20 00:541
525-Apr-20 00:594
625-Apr-20 01:071
725-Apr-20 01:132
825-Apr-20 01:294
925-Apr-20 01:334
1025-Apr-20 01:414
1125-Apr-20 01:524
1225-Apr-20 02:122
1325-Apr-20 02:555
1425-Apr-20 03:003
1525-Apr-20 03:082
1625-Apr-20 03:161
1725-Apr-20 03:235
1825-Apr-20 03:555
1925-Apr-20 04:114
2025-Apr-20 08:591
2125-Apr-20 09:045
2225-Apr-20 09:103
2325-Apr-20 09:472
2425-Apr-20 09:531
2525-Apr-20 09:582
2625-Apr-20 10:125
2725-Apr-20 10:344
2825-Apr-20 12:413
29
Sheet1
Cell Formulas
RangeFormula
D2D2=CountBelow(A2:B28,5,2)
 
Upvote 0
Your macro looks very promising [B]Peter_SSs[/B]. And yes I want to count the number of times the value in column B has lasted longer that 2 hours (column A).

I tried to implement your code with my spreadsheet, but I must be incorporating my values incorrectly. When I play around with cell D2 limits, the resulting count doesn't seem to be accurate.

For my column B limit, I'm actually looking at a limit of 0.04. See the attached screenshot of a small sample of my data. The screen shot data is above my threshold of 0.04, but I have over 10000 rows that need to be evaluated. Also, does my date/time format in column A need to be a certain cell type?

I really appreciate the help!
 

Attachments

  • Screen Shot 2020-04-28 at 8.17.58 AM.png
    Screen Shot 2020-04-28 at 8.17.58 AM.png
    185.6 KB · Views: 3
Upvote 0
does my date/time format in column A need to be a certain cell type?
It needs to be a 'proper' date (that is, a number) not text. In another cell, what does this formula return?
=ISNUMBER(A2)

If you are still having problems, can you post a small set of sample data with XL2BB and explain your expected result in relation to that small sample? Make sure your sample includes data that would result in a count of say 2.
 
Upvote 0
Thanks! I don’t think the date is my problem. Within your code, which values would I change if I wanted to know if the column B data dropped below 0.04. For example, if the value in column B dropped to 0.039, I’d like to start the two hour timer. Seems like maybe the current code subtracts 1 but I’m not positive.
 
Upvote 0
Within your code, which values would I change if I wanted to know if the column B data dropped below 0.04
I had only allowed for whole numbers, so change the function to this

Rich (BB code):
Function CountBelow(rngData As Range, dblLimit As Double, dblHours As Double) As Long
  Dim a As Variant
  Dim i As Long
  Dim dteStart As Date
  Dim blnStarted As Boolean

  a = rngData.Value
  For i = 1 To UBound(a)
    If a(i, 2) < dblLimit Then
      If Not blnStarted Then
        blnStarted = True
        dteStart = a(i, 1)
      End If
    Else
      If blnStarted Then
        If a(i - 1, 1) - dteStart >= dblHours / 24 Then CountBelow = CountBelow + 1
      End If
      blnStarted = False
    End If
  Next i
  If blnStarted Then
    If a(i - 1, 1) - dteStart >= 1 / 12 Then CountBelow = CountBelow + 1
  End If
End Function

Then you would use
=CountBelow(A2:B28,0.04,2)
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,869
Members
449,054
Latest member
juliecooper255

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