If Both C=1 And D>0 ,then ... ?

ibmy

Board Regular
Joined
Mar 4, 2020
Messages
134
Office Version
  1. 2016
Platform
  1. Windows
Greetings experienced,

With my limited knowledge of Excel, only know basics, I was thinking to use function
"=IF(AND(N4=1,O3>0), , ) " & "(INDEX(O:O,ROW()-O3-1" ,in formula but do not know how to structure/complete it.

When cell in Column N =1 and above row of it, cell in Column O >0 , assign "1" in cell Column P above row Column O, equal to Value in Column O + 1

Column M = Data
Column N = Confirmation
Column O = Total number of 0
Column P = Expected Result

cell 431.xlsb
MNOP
1KYes/NoTotal 0Expected Result
2
3-0.31
40
50
60
704
80.111
901
101.31
11-0.4
120
130
1403
15-1.8
16-0.11
170
1802
19-0.11
200.9
2101
220.3
23-0.2
Sheet1

Cell O14 and O21 show total number 0 but N15 and N 22 show no =1 , so no need to assign "1" in Column P

Sample taken from 300k row data.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi @ibmy

Could you explain the rules with a couple of examples from your minisheet?
When cell in Column N =1 and above row of it, cell in Column O >0 , assign "1" in cell Column P above row Column O, equal to Value in Column O + 1

For example, in cell N8 there is a 1 and in cell O7 there is a 4 (it is > 0), then the rules are fulfilled.

But your "expected result" is not clear to me.

As well as that example you can explain 2 or 3 examples with the result that you want to understand.



Edit.

I now understand where the result goes:
1684682055960.png


--------------
I hope to hear from you soon.
Respectfully
Dante Amor
--------------​
 
Last edited:
Upvote 0
Try placing the following array formula (to be entered using Ctrl+Shift+Enter, not just Enter) in cell P3 and drag-copying it down as needed:
Excel Formula:
=IFERROR(IF(AND(M3<>0,INDEX(N4:N103,MATCH(TRUE,M4:M103<>0,0))=1,INDEX(O4:O103,MATCH(TRUE,M4:M103<>0,0)-1)>0),1,""),"")
Note 1: I have assumed your longest streak of zeros in column M is 100. If it's shorter/longer, adjust ranges in the formula as needed.
Note 2: The formula might be slow for 300k rows of data.
 
Upvote 0
@DanteAmor , sorry for not explain the part of when the expected result is "1". My bad for only explain the part of when no need to sign "1" in Column P , not clearly enough.

No assign "1" in Column P : Cell O14 and O21 show total number 0 ( 3 , 1 ) but N15 and N22 show no =1
Assign "1" in Column P : Cell O7 ,O9 , O18 show total number 0 ( 4 , 1 , 2) ) and N8, N10 , N19 show = 1


@Tetra201 , Thanks for the formula. It runs not too fast or too slow but it is fine for 400k row data. The lower longest streak of zeros in column M I set in formula , the faster it runs.

I have 2 questions :

1 ) May I know what use of Column M in formula? I thought only need column N and column O in formula.

2) What if I insert 1 cell in Column P so the data look like this :

cell 431.xlsb
MNOP
1KYes/NoTotal 0Expected Result
2
3-0.31
40
50
60
70
80.1141
90
101.311
11-0.4
120
130
140
15-1.83
16-0.11
170
180
19-0.112
200.9
210
220.31
23-0.2
Sheet1

Can the formula be shorter and simpler? or maybe faster a bit...
 
Upvote 0
2) What if I insert 1 cell in Column P so the data look like this :
or maybe faster a bit...
Considering your comment from the previous post, the 1 and > 0 are in the same row.
If you are interested, with the following macro the result is immediate.

But I have a question, if there was a scenario where the first value is 0, for example:
varios 22may2023.xlsm
ABCD
1KYes/NoTotal 0Expected Result
21
30
40.1111
50
60
70
80.1131
90
101.311
Hoja1


So the result should start in cell D2.
Please try the following macro:
VBA Code:
Sub CountZero_v1()
  Dim a As Variant
  Dim i As Long, j As Long
  a = Range("A3:C" & Range("A" & Rows.Count).End(3).Row).Value
  ReDim b(0 To UBound(a), 1 To 1)
  For i = UBound(a) To 1 Step -1
    If a(i, 2) = 1 And a(i, 3) > 0 Then
      b(i - a(i, 3) - 1, 1) = 1
    End If
  Next
  Range("D2").Resize(UBound(b)).Value = b
End Sub

--------------------------​
But if the above scenario does not exist, then also the following macro works.
VBA Code:
Sub CountZero_v2()
  Dim a As Variant
  Dim i As Long, j As Long
  a = Range("A3:C" & Range("A" & Rows.Count).End(3).Row).Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = UBound(a) To 1 Step -1
    If a(i, 2) = 1 And a(i, 3) > 0 Then
      b(i - a(i, 3) - 1, 1) = 1
    End If
  Next
  Range("d3").Resize(UBound(b)).Value = b
End Sub

--------------------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 0
Solution
1 ) May I know what use of Column M in formula? I thought only need column N and column O in formula.
Here is how the formula works. It checks three conditions:
i) Is value in cell (formula row, column M) different from zero?
ii) Is value in cell (first row after the formula row with a non-zero value in column M, column N) equal to 1?
iii) Is value in cell (row immediately before the first row after the formula row with a non-zero value in column M, column O) greater than zero?
If each of the three conditions evaluates to TRUE, the formula returns 1, otherwise it returns an empty string or an error, which is trapped by the IFERROR wrapper.

2) What if I insert 1 cell in Column P... Can the formula be shorter and simpler? or maybe faster a bit...
In this case, the formula becomes just two-symbol shorter:
Excel Formula:
=IFERROR(IF(AND(M3<>0,INDEX(N4:N103,MATCH(TRUE,M4:M103<>0,0))=1,INDEX(O4:O103,MATCH(TRUE,M4:M103<>0,0))>0),1,""),"")
 
Upvote 0
@Tetra201 ,Thanks for explaination and shorter formula. The formula working great I am really appreaciate it

@DanteAmor ,Thanks for the macro. It working great.

Also, thanks you for details of how thinking another situation where there is might result in row 2.
So far, the data show no result in row 2 yet and only interested the result in the middle of data ( above row 2 ).
Then, I will be using "Sub CountZero_v2()" macro for now.

I have a question and need confirmation, is this the right way to change code if I want to use Column M, N, O, P ?

VBA Code:
Sub CountZero_v2()
 
  Dim a As Variant
  Dim i As Long, j As Long
 
  a = Range("M3:O" & Range("M" & Rows.Count).End(3).Row).Value
 
  ReDim b(1 To UBound(a), 1 To 1)
  For i = UBound(a) To 1 Step -1
    If a(i, 2) = 1 And a(i, 3) > 0 Then
      b(i - a(i, 3) - 1, 1) = 1
    End If
  Next
  Range("P3").Resize(UBound(b)).Value = b
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,112
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