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

ibmy

Board Regular
Joined
Mar 4, 2020
Messages
134
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

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
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
@Alex Blakenburg , Been using your code almost 1 month in experimenting data, so far the code is great. Thanks.
I have 4 questions regarding the code..

1)
VBA Code:
Dim compareToA As Variant, compareToB

Does the code missing declare compareToB as Variant ?

2)
VBA Code:
compareToA = CDec(Application.Round(arr(1, 1), 8))

What value "8" refer to?

3)
VBA Code:
If valA < compareToB And valB < compareToB Or valB > compareToA And valA > compareToA Then

Is this the right way to write 2 condition for OR function in this code? :

Condition 1 : valA less than compareToB AND valB less than compareToB
Or
Condition 2 : valB more than compareToA AND valA more than compareToA
So far the result is right but need your confirmation if I missing any other line to edit/change?

4)
VBA Code:
rowLast = .Range("D" & Rows.Count).End(xlUp).Row
VBA Code:
Set rng = .Range("D3", .Cells(rowLast, "H"))
VBA Code:
rng.Columns(5).ClearContents
VBA Code:
compareToB = CDec(Application.Round(arr(1, 5), 8))
VBA Code:
valB = CDec(Application.Round(arr(i, 5), 8))
VBA Code:
arr(i, 5) = 1
VBA Code:
rng.Columns(5).Value = Application.Index(arr, 0, 5)

If I change data column placement of C,D,E to D,F,H , does these 7 lines need to change or there are any other lines need to edit/change?
So far the result is ok but need your confirmation.
 
Upvote 0
1) compareToB
Yes omitting the type in the Dim statement means it defaults to variant. I would have peferred to actually have declared it "as Variant" so it works but was an oversight on my part.

2) compareToA = CDec(Application.Round(arr(1, 1), 8))
I was trying to head avoid having a floating point error by applying rounding that would not impact that would not comprise the number of significant digits required and decided on 8 decimal places. The Decimal data type does not use the floating point method so should not introduce further floating point errors.

3) And and Or in If statement.
I believe it would work but for clarity I would put brackets around each of the And pairings
If (valA < compareToB And valB < compareToB) Or (valB > compareToA And valA > compareToA) Then

4) Only the "compareToB =" and "ValB =" lines look to be wrong.
Original range C,D,E and loaded into arr as 3 columns
New range D,E,F,G,H (although you are only interested in the 3 colums D,F,H) all 5 columns are loaded to arr.
D is column 1 in "arr", F is column 3, H is column 5.
compareToB and ValB now refer to column F so should both refer to arr(i,3)
 
Upvote 1

Forum statistics

Threads
1,223,936
Messages
6,175,499
Members
452,650
Latest member
Tinfish

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