Count consecutive cells that matching a criteria, Starting from the very first cell

Shameer

New Member
Joined
Oct 1, 2023
Messages
5
Office Version
  1. 365
Platform
  1. MacOS
Hello,
In the worksheet uploaded as image,

I need to count
- starting from very First row
- Consecutive occurrences
- if value is greater than 1% and less than 3%

In the uploaded example, the answer would be 1.

Appreciate feedback.
 

Attachments

  • Picture1.png
    Picture1.png
    18.5 KB · Views: 23

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Sham Let's get the ball rollin. We have to start somewhere. Now I modified your example, but the first 5 numbers should be the same. I have a question on your answer of 1. Your criteria of consecutive occurrences of values greater than 1% and less than 3% doesn't add up when 2.5, 1.31 and 2.18 would be 2 times. Unless I am misunderstanding your criteria, which is what I normally do, the example below answer should be 3. So let the question, comments or suggestions begin. We can always modify the solution to fit any criteria.

VBA Code:
Sub Prog1()

Dim LastCel As Integer
Dim count1 As Integer
count1 = 0

LastCel = Cells(Rows.Count, "C").End(xlUp).Row

For i = 5 To LastCel

Cells(i, 3).Select
If Cells(i, 3) > 0.01 And Cells(i, 3) < 0.03 And Cells(i + 1, 3) > 0.01 And Cells(i + 1, 3) < 0.03 Then

MsgBox Cells(i, 3)
count1 = count1 + 1

End If

Next i

Range("D3") = count1

End Sub

23-10-01 work 1.xlsm
BCD
3Count is3
4
5246.381.56%
6240.54.04%
7244.122.50%
8246.991.31%
9244.882.18%
10255.75.00%
11262.594.00%
12266.51.25%
13265.282.30%
14274.398.00%
Data
 
Upvote 0
Thanx for the lightning response #Exguy4u!!.
I guess I did not explain it well (which is I what I normally do :)).

In the Column "Variations compared to Today", I need to count:
- cells that meet the criteria 'greater than 1% and less than 3%'
- starting from very First cell
- up to the cell after which the criteria fails

In the example I uploaded earlier, first cell of column meets the criteria. Second cell of column fails the criteria. So the count is 1.

In the new example I am uploading with this reply, the count would be be 3 (first 3 cells meets the criteria and 4th cell fails the criteria)).

Trust this explanation makes my question clearer.

Fyi, I see a very old post from 2017 that seems very similar to my question. My knowledge was not enough to adapt that formula for my need.

Thanx again.
 

Attachments

  • Picture1.png
    Picture1.png
    18.8 KB · Views: 10
Upvote 0
Sham, I believe I have covered all the different scenarios. Now the thing about this web site, in my opinion, is that how we ask the question to solve our problems is a big part. Once the community sees your input and output, using this XL2BB, they have a better idea of other solutions. So maybe an A+ student will weigh in and show us a better solution. In the meantime, this program should give you what you want.

VBA Code:
Sub Prog3()

Dim LastCel As Integer
Dim count1 As Integer
Dim Trap As Boolean

Trap = False
count1 = 0

LastCel = Cells(Rows.Count, "C").End(xlUp).Row

If Cells(5, 3) > 0.01 And Cells(5, 3) < 0.03 Then
    Trap = True
End If

For i = 5 To LastCel

Cells(i, 3).Select

If Cells(i, 3) > 0.01 And Cells(i, 3) < 0.03 And Trap = True Then
    Trap = True
    count1 = count1 + 1
End If

If count1 >= 1 And Trap = False Then
    Exit For
End If

If Cells(i + 1, 3) > 0.01 And Cells(i + 1, 3) < 0.03 Then
    Trap = True
Else
    Trap = False
End If

Next i

Range("D3") = count1

End Sub

23-10-01 work 2.xlsm
BCDEFGH
3Count is1
4
5265.28-9.35%-8.81%-9.35%2.18%-9.35%
6274.392.50%-9.35%-7.77%-7.77%2.50%
7246.38-7.77%-7.77%2.50%2.50%-7.77%
8240.5-5.68%2.50%-5.68%-5.68%-5.68%
9244.121.31%1.31%1.31%1.31%1.31%
10246.992.18%2.18%2.18%-9.35%2.18%
11244.88-2.14%-2.14%-2.14%-2.14%-2.14%
12255.7-4.71%-4.71%-4.71%-4.71%-4.71%
13262.59-6.11%-6.11%-6.11%-6.11%-6.11%
14266.5-8.81%-5.68%-8.81%-8.81%-8.81%
Data 3
 
Upvote 0
Formula solution.
If you need VBA, I will come back.
Book2
ABC
13
2
3
4250.22244.122.50%
5246.991.31%
6244.882.18%
7255.7-2.14%
82.50%
91.31%
105.00%
11-1.00%
Sheet1
Cell Formulas
RangeFormula
C1C1=AGGREGATE(15,6,(ROW($C$4:$C$11)-4)/(($C$4:$C$11<0.01)+($C$4:$C$11>0.03)>0),1)
 
Upvote 1
Solution
Formula solution.
If you need VBA, I will come back.
Book2
ABC
13
2
3
4250.22244.122.50%
5246.991.31%
6244.882.18%
7255.7-2.14%
82.50%
91.31%
105.00%
11-1.00%
Sheet1
Cell Formulas
RangeFormula
C1C1=AGGREGATE(15,6,(ROW($C$4:$C$11)-4)/(($C$4:$C$11<0.01)+($C$4:$C$11>0.03)>0),1)

Amazing....

Two simple questions.

I want to make the formula portable to start from any row. So, I changed the hard coded "-4" (see below image) to 'cell reference' (e.g., ROW(C4)).
Is that correct and will it work?? (It seems to work, but I prefer to have the Author's confirmation)
1696500108366.png


Second, in order to achieve the same results from a row of data (instead of column of data), will changing row to column, work (like in below example)??
1696500898219.png
 
Upvote 0
Another possible formula option.

23 10 05.xlsm
C
13
2
3
42.50%
51.31%
62.18%
7-2.14%
82.50%
92.31%
105.00%
11-1.00%
12
Shameer
Cell Formulas
RangeFormula
C1C1=IFNA(MATCH(TRUE,ABS(C4:C11-0.02)>=0.01,0)-1,ROWS(C4:C11))
 
Upvote 0
I want to make the formula portable to start from any row. So, I changed the hard coded "-4" (see below image) to 'cell reference' (e.g., ROW(C4)).
Is that correct and will it work?? (It seems to work, but I prefer to have the Author's confirmation)
Yes, of course.
Second, in order to achieve the same results from a row of data (instead of column of data), will changing row to column, work (like in below example)??
View attachment 99784
Maybe. Try it!
 
Upvote 0
Sham, I believe I have covered all the different scenarios. Now the thing about this web site, in my opinion, is that how we ask the question to solve our problems is a big part. Once the community sees your input and output, using this XL2BB, they have a better idea of other solutions. So maybe an A+ student will weigh in and show us a better solution. In the meantime, this program should give you what you want.

VBA Code:
Sub Prog3()

Dim LastCel As Integer
Dim count1 As Integer
Dim Trap As Boolean

Trap = False
count1 = 0

LastCel = Cells(Rows.Count, "C").End(xlUp).Row

If Cells(5, 3) > 0.01 And Cells(5, 3) < 0.03 Then
    Trap = True
End If

For i = 5 To LastCel

Cells(i, 3).Select

If Cells(i, 3) > 0.01 And Cells(i, 3) < 0.03 And Trap = True Then
    Trap = True
    count1 = count1 + 1
End If

If count1 >= 1 And Trap = False Then
    Exit For
End If

If Cells(i + 1, 3) > 0.01 And Cells(i + 1, 3) < 0.03 Then
    Trap = True
Else
    Trap = False
End If

Next i

Range("D3") = count1

End Sub

23-10-01 work 2.xlsm
BCDEFGH
3Count is1
4
5265.28-9.35%-8.81%-9.35%2.18%-9.35%
6274.392.50%-9.35%-7.77%-7.77%2.50%
7246.38-7.77%-7.77%2.50%2.50%-7.77%
8240.5-5.68%2.50%-5.68%-5.68%-5.68%
9244.121.31%1.31%1.31%1.31%1.31%
10246.992.18%2.18%2.18%-9.35%2.18%
11244.88-2.14%-2.14%-2.14%-2.14%-2.14%
12255.7-4.71%-4.71%-4.71%-4.71%-4.71%
13262.59-6.11%-6.11%-6.11%-6.11%-6.11%
14266.5-8.81%-5.68%-8.81%-8.81%-8.81%
Data 3

Thanx again #Exguy4u. I am an absolute rookie in VBA and could not implement your suggestion properly. I am sure someone else would find it useful in the near future.
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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