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 | |||||
---|---|---|---|---|---|
B | C | D | |||
3 | Count is | 3 | |||
4 | |||||
5 | 246.38 | 1.56% | |||
6 | 240.5 | 4.04% | |||
7 | 244.12 | 2.50% | |||
8 | 246.99 | 1.31% | |||
9 | 244.88 | 2.18% | |||
10 | 255.7 | 5.00% | |||
11 | 262.59 | 4.00% | |||
12 | 266.5 | 1.25% | |||
13 | 265.28 | 2.30% | |||
14 | 274.39 | 8.00% | |||
Data |
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 | |||||||||
---|---|---|---|---|---|---|---|---|---|
B | C | D | E | F | G | H | |||
3 | Count is | 1 | |||||||
4 | |||||||||
5 | 265.28 | -9.35% | -8.81% | -9.35% | 2.18% | -9.35% | |||
6 | 274.39 | 2.50% | -9.35% | -7.77% | -7.77% | 2.50% | |||
7 | 246.38 | -7.77% | -7.77% | 2.50% | 2.50% | -7.77% | |||
8 | 240.5 | -5.68% | 2.50% | -5.68% | -5.68% | -5.68% | |||
9 | 244.12 | 1.31% | 1.31% | 1.31% | 1.31% | 1.31% | |||
10 | 246.99 | 2.18% | 2.18% | 2.18% | -9.35% | 2.18% | |||
11 | 244.88 | -2.14% | -2.14% | -2.14% | -2.14% | -2.14% | |||
12 | 255.7 | -4.71% | -4.71% | -4.71% | -4.71% | -4.71% | |||
13 | 262.59 | -6.11% | -6.11% | -6.11% | -6.11% | -6.11% | |||
14 | 266.5 | -8.81% | -5.68% | -8.81% | -8.81% | -8.81% | |||
Data 3 |
Formula solution.
If you need VBA, I will come back.
Book2
A B C 1 3 2 3 4 250.22 244.12 2.50% 5 246.99 1.31% 6 244.88 2.18% 7 255.7 -2.14% 8 2.50% 9 1.31% 10 5.00% 11 -1.00% Sheet1
Cell Formulas Range Formula C1 C1 =AGGREGATE(15,6,(ROW($C$4:$C$11)-4)/(($C$4:$C$11<0.01)+($C$4:$C$11>0.03)>0),1)
Yes, of course.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)
Maybe. Try it!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
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
B C D E F G H 3 Count is 1 4 5 265.28 -9.35% -8.81% -9.35% 2.18% -9.35% 6 274.39 2.50% -9.35% -7.77% -7.77% 2.50% 7 246.38 -7.77% -7.77% 2.50% 2.50% -7.77% 8 240.5 -5.68% 2.50% -5.68% -5.68% -5.68% 9 244.12 1.31% 1.31% 1.31% 1.31% 1.31% 10 246.99 2.18% 2.18% 2.18% -9.35% 2.18% 11 244.88 -2.14% -2.14% -2.14% -2.14% -2.14% 12 255.7 -4.71% -4.71% -4.71% -4.71% -4.71% 13 262.59 -6.11% -6.11% -6.11% -6.11% -6.11% 14 266.5 -8.81% -5.68% -8.81% -8.81% -8.81% Data 3
Thank you for your time and effortsYes, of course.
Maybe. Try it!