Keep assign "1" unless A more than "x" or B les than "y" or Both

ibmy

Board Regular
Joined
Mar 4, 2020
Messages
108
Office Version
  1. 2016
Platform
  1. Windows
Hi,

Explanation in visual :

less more.jpg


Explanation in text:
1) Keep assign "1" in column E if "A" value less than or same as starting "A" value AND "B" value more than or same as starting "B" value.
2) Stop assign "1" in column E if "A" value more than starting "A" value OR "B" value less than starting "B" value OR both happened.

less more.xlsx
CDEFG
1"A""B"Resultexplanation:
21.190481.19044Start here, "A" number : 1.19048 , "B" number : 1.19044
31.190451.190441
41.190461.190441
51.190461.190451
61.190471.190461
71.190481.19043"B" number less than 1.19044. New "A" number : 1.19048 , "B" number : 1.19043
81.190491.19045"A" number more than 1.19048. New "A" number : 1.19049, "B" number : 1.19045
91.190481.190461
101.190481.190471
111.190501.19042"A" number more than 1.19049 and "B" number less than 1.19045. New "A" number : 1.19050, "B" number : 1.19042
121.190491.190451
131.190481.190471
141.190461.190441
151.190471.190451
161.190471.190461
171.190531.19044"A" number more than 1.19050. New "A" number : 1.19053, "B" number : 1.19044
181.190471.190441
191.190501.190451
201.190441.19039"B" number less than 1.19044. New "A" number : 1.19044 , "B" number : 1.19039
211.190451.19028"A" number more than 1.19044 and "B" number less than 1.19039. New "A" number : 1.19045, "B" number : 1.19028
221.190601.19027"A" number more than 1.19045. New "A" number : 1.19060, "B" number : 1.19027
Sheet5

Prefer VBA code solution but if formula solution can get 250k row data get result faster, dont matter.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
MrExcelPlayground22.xlsx
CDEFG
1"A""B"ResultResultexplanation:
21.190481.19044Start here, "A" number : 1.19048 , "B" number : 1.19044
31.190451.1904411
41.190461.1904411
51.190461.1904511
61.190471.1904611
71.190481.19043 "B" number less than 1.19044. New "A" number : 1.19048 , "B" number : 1.19043
81.190491.19045 "A" number more than 1.19048. New "A" number : 1.19049, "B" number : 1.19045
91.190481.1904611
101.190481.1904711
111.19051.19042 "A" number more than 1.19049 and "B" number less than 1.19045. New "A" number : 1.19050, "B" number : 1.19042
121.190491.1904511
131.190481.1904711
141.190461.1904411
151.190471.1904511
161.190471.1904611
171.190531.19044 "A" number more than 1.19050. New "A" number : 1.19053, "B" number : 1.19044
181.190471.1904411
191.19051.1904511
201.190441.19039 "B" number less than 1.19044. New "A" number : 1.19044 , "B" number : 1.19039
211.190451.19028 "A" number more than 1.19044 and "B" number less than 1.19039. New "A" number : 1.19045, "B" number : 1.19028
221.19061.19027 "A" number more than 1.19045. New "A" number : 1.19060, "B" number : 1.19027
Sheet12
Cell Formulas
RangeFormula
F3:F22F3=IF(OR(C3>MAX(C$2:C2),D3<MIN(D$2:D2)),"",1)
 
Upvote 0
@JamesCanale Thanks for reply and the formula.

There some row assigned "1" wrongly.

My bad, I forgot to highlight that if next row "A" value more than base "A" value OR next row "B" value less than base "B" value OR both happened then there are new base "A" value and base "B" value (row 7, 8 , 11, 17, 20 , 21 ,22 )
 
Upvote 0
If after trying James's suggestion you find that you still need VBA, then give this a try:

VBA Code:
Sub FlagRows()

    Dim ws As Worksheet
    Dim rng As Range
    Dim arr As Variant
    Dim compareToA As Variant, compareToB
    Dim valA As Variant, valB As Variant
    Dim rowLast As Long, i As Long
    
    Set ws = ActiveSheet            ' Ideally name the sheet using Worksheets("YourSheetName")
    With ws
        rowLast = .Range("C" & Rows.Count).End(xlUp).Row
        Set rng = .Range("C2", .Cells(rowLast, "E"))
        rng.Columns(3).ClearContents
        arr = rng.Value2
    End With
    
    compareToA = CDec(Application.Round(arr(1, 1), 8))
    compareToB = CDec(Application.Round(arr(1, 2), 8))
    
    For i = 2 To UBound(arr)
        valA = CDec(Application.Round(arr(i, 1), 8))
        valB = CDec(Application.Round(arr(i, 2), 8))
        If valA <= compareToA And valB >= compareToB Then
            arr(i, 3) = 1
        Else
            compareToA = CDec(Application.Round(arr(i, 1), 8))
            compareToB = CDec(Application.Round(arr(i, 2), 8))
        End If
    Next i

    rng.Columns(3).Value = Application.Index(arr, 0, 3)
End Sub
 
Upvote 1
Solution
@Alex Blakenburg Thanks for reply and the code. The code works perfectly.

Thank you very much for spend time writing the code, I appreciate it.
I wish you have a great day.
 
Upvote 0

Forum statistics

Threads
1,215,454
Messages
6,124,933
Members
449,195
Latest member
Stevenciu

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