ibmy
Board Regular
- Joined
- Mar 4, 2020
- Messages
- 134
- Office Version
- 2016
- Platform
- Windows
Hi Mr Excel,
How do find Number Pattern in data which is :
1. Starting a Negative value ( - )
2. Follow by at least a Zero ( 0 / 00 / 000 / 000..... )
3. Follow by a Positive value ( + )
4. End with a Negative value ( - )
Number Pattern : - 0 + -
When Number Pattern found, assign "1" at row of Positive Value.
Sample data of 200k row with description :
This is my current solution but need 2 columns :
Column G, I use this code to find number pattern - 0 +
Then, I use formula " =IF(AND(G6=1,F7<0),1,"") " to find what value of end pattern.
Thanks in advance, hopefully I can reduce column used in sheet and improve my work efficiency.
How do find Number Pattern in data which is :
1. Starting a Negative value ( - )
2. Follow by at least a Zero ( 0 / 00 / 000 / 000..... )
3. Follow by a Positive value ( + )
4. End with a Negative value ( - )
Number Pattern : - 0 + -
When Number Pattern found, assign "1" at row of Positive Value.
Sample data of 200k row with description :
4 no.xlsb | |||||||
---|---|---|---|---|---|---|---|
F | I | J | |||||
1 | Data | Expected Result | Explanation | ||||
2 | |||||||
3 | 0.3 | ||||||
4 | -0.1 | ||||||
5 | 0 | ||||||
6 | 0.9 | 1 | |||||
7 | -0.7 | Pattern found. Assign "1" at Positive Value & Start new find at this row | |||||
8 | -1.7 | ||||||
9 | -1.1 | ||||||
10 | -1 | ||||||
11 | 0 | ||||||
12 | 0 | ||||||
13 | 0 | ||||||
14 | 0.2 | 1 | |||||
15 | -1.8 | Pattern found. Assign "1" at Positive Value & Start new find at this row | |||||
16 | 0 | ||||||
17 | 0 | ||||||
18 | 1 | 1 | |||||
19 | -2 | Pattern found. Assign "1" at Positive Value & Start new find at this row | |||||
20 | -0.6 | ||||||
21 | 0 | ||||||
22 | 1.3 | ||||||
23 | 1.2 | ||||||
24 | -0.5 | ||||||
25 | -1.9 | ||||||
26 | -2 | ||||||
27 | 0.4 | ||||||
28 | -0.1 | ||||||
29 | 0 | ||||||
30 | 1.2 | ||||||
31 | 0 | ||||||
32 | -0.3 | ||||||
33 | 0 | ||||||
34 | 0 | ||||||
35 | 0.4 | ||||||
36 | 0 | ||||||
37 | 0.7 | ||||||
38 | -0.7 | ||||||
Sheet1 |
This is my current solution but need 2 columns :
4 no.xlsb | |||||
---|---|---|---|---|---|
F | G | H | |||
1 | Data | Code | Function IF-AND | ||
2 | |||||
3 | 0.3 | ||||
4 | -0.1 | ||||
5 | 0 | ||||
6 | 0.9 | 1 | 1 | ||
7 | -0.7 | ||||
8 | -1.7 | ||||
9 | -1.1 | ||||
10 | -1 | ||||
11 | 0 | ||||
12 | 0 | ||||
13 | 0 | ||||
14 | 0.2 | 1 | 1 | ||
15 | -1.8 | ||||
16 | 0 | ||||
17 | 0 | ||||
18 | 1 | 1 | 1 | ||
19 | -2 | ||||
20 | -0.6 | ||||
21 | 0 | ||||
22 | 1.3 | 1 | |||
23 | 1.2 | ||||
24 | -0.5 | ||||
25 | -1.9 | ||||
26 | -2 | ||||
27 | 0.4 | ||||
28 | -0.1 | ||||
29 | 0 | ||||
30 | 1.2 | 1 | |||
31 | 0 | ||||
32 | -0.3 | ||||
33 | 0 | ||||
34 | 0 | ||||
35 | 0.4 | 1 | |||
36 | 0 | ||||
37 | 0.7 | ||||
38 | -0.7 | ||||
Sheet2 |
Cell Formulas | ||
---|---|---|
Range | Formula | |
H6:H38 | H6 | =IF(AND(G6=1,F7<0),1,"") |
Column G, I use this code to find number pattern - 0 +
VBA Code:
Sub MinusZeroPlus()
Dim a As Variant, b As Variant
Dim i As Long
Dim bStart As Boolean, bCont As Boolean
a = Range("F2", Range("F" & Rows.Count).End(xlDown)).Value
ReDim b(1 To UBound(a), 1 To 1)
For i = 1 To UBound(a)
Select Case a(i, 1)
Case Is < 0
bStart = True
bCont = False
Case 0
If bStart Then bCont = True
Case Is > 0
If bCont Then b(i, 1) = 1
bStart = False
bCont = False
End Select
Next i
Range("G2").Resize(UBound(b)).Value = b
End Sub
Then, I use formula " =IF(AND(G6=1,F7<0),1,"") " to find what value of end pattern.
Thanks in advance, hopefully I can reduce column used in sheet and improve my work efficiency.