Identify Number Pattern of Positive(+) -> Zero(0) -> Negative(-)

ibmy

Board Regular
Joined
Mar 4, 2020
Messages
106
Office Version
  1. 2016
Platform
  1. Windows
Dear Smartest Excelers,

Some of sample from my 90k row data:

Book1
ABCD
1DiffResult
20
30.4
40.5
52.3
60
70
80
9-3.91B5(+) -> B6:B8(0) -> B9(-)
10-0.1Rule 4: Start new find
110
120.4No count : B10(-) -> B11 (0) -> B12 (+)
130
14-0.51B12(+) -> B13(0) -> B14(-)
150Rule 4: Start new find
16-0.2
170.3
180
190
200
210
220.3No count : B17(+) -> B18:B21 (0) -> B22 (+)
23-0.5
240
25-0.6No count : B23(-) -> B24 (0) -> B25 (-)
260.9
270.1
280
290
30-0.11B27(+) -> B28:B29 (0) -> B30(-)
31-0.1Rule 4: Start new find
320.4
Sheet17


Rule:
1. Must start from a Positive Value
2. Follow by 0 ( at least one 0, no limit how many 0 can occur but for now, the max 0's I dealt is 50, might change later)
3. Ending with a Negative Value
4. After Number Pattern is complete, number "1" assigned and start a new find in next row (Rule 1).

Short : Before 0 must postive value & after 0 , must negative value ( + 0 - )

......

No count / Break Rule :
1. Not start from Positive Value = Not count
2. Start a Positive Value -> Follow by no 0 = No count
3. Start a Positive Value -> Follow by 0 -> Ending with not a Negative Value = Break Rule

Short : Invalid number pattern (- 0 + / - 0 - / + 0 + )

.....

Any outcome is welcome,(other than result in Column C) as long as I know which Row happen this kind of number pattern. I was thinking, I will do filter to "1" after get a result from Column C.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi, @ibmy
Interesting case, try this:
VBA Code:
Sub a1159856a()
'https://www.mrexcel.com/board/threads/identify-number-pattern-of-positive-zero-0-negative.1159856/

Dim i As Long, j As Long, n As Long
Dim va, z
Dim x As Double

n = Range("B" & Rows.Count).End(xlUp).Row + 1
va = Range("B1:B" & n)
Range("C2:C" & n).ClearContents
ReDim z(1 To 3)

For i = 2 To UBound(va, 1) - 1
    x = va(i, 1)
    
    Select Case x
        Case Is > 0
           If va(i + 1, 1) = 0 Then z(1) = x
        Case 0
           If va(i + 1, 1) < 0 And Len(z(1)) <> 0 Then z(2) = 0
        Case Is < 0
           If va(i - 1, 1) = 0 And Len(z(1)) <> 0 Then z(3) = x Else z(1) = Empty: z(2) = Empty
    End Select

    If Len(z(1)) <> 0 And Len(z(2)) <> 0 And Len(z(3)) <> 0 Then
        Cells(i, "C") = 1
        z(1) = Empty: z(2) = Empty: z(3) = Empty
    End If

Next
End Sub

Book2
BC
1Diff
20
30.4
40.5
52.3
60
70
80
9-3.91
10-0.1
110
120.4
130
14-0.51
150
16-0.2
170.3
180
190
200
210
220.3
23-0.5
240
25-0.6
260.9
270.1
280
290
30-0.11
31-0.1
320.4
Sheet2
 
Upvote 0
Thanks @Akuini , VBA is working and fast (y)

May I know how about pattern ( - 0 + )?
Same rule as above.

With my zero knowledge of VBA, I tried to modify VBA a bit but did not work. ?


Attemp 1 Fail : (Change the order)

Select Case x
Case Is < 0
If va(i - 1, 1) = 0 And Len(z(1)) <> 0 Then z(3) = x Else z(1) = Empty: z(2) = Empty
Case 0
If va(i + 1, 1) < 0 And Len(z(1)) <> 0 Then z(2) = 0
Case Is > 0
If va(i + 1, 1) = 0 Then z(1) = x
End Select

Attemp 2 Fail : (Change condition inside case >0 and case <0 )

Select Case x
Case Is < 0
If va(i - 1, 1) = 0 Then z(1) = x
Case 0
If va(i + 1, 1) < 0 And Len(z(1)) <> 0 Then z(2) = 0
Case Is > 0
If va(i + 1, 1) = 0 And Len(z(1)) <> 0 Then z(3) = x Else z(1) = Empty: z(2) = Empty
End Select
 
Upvote 0
Hi here is with formula!
you will need two rows of headers for formula .

TempO.xlsx
AB
1
2Diff
30 
40.4 
50.5 
62.3 
70 
80 
90 
10-3.91
11-0.1 
120 
130.4 
140 
15-0.51
160 
17-0.2 
180.3 
190 
200 
210 
220 
230.3 
24-0.5 
250 
26-0.6 
270.9 
280.1 
290 
300 
31-0.11
32-0.1 
330.4 
18
Cell Formulas
RangeFormula
B3:B33B3=IF(IF(AND((AND(A2>0,A3=0)+AND(A2=0,A1>=0)+AND(A2<0,A1=0,A3<=0))=1,(AND(A3>0,A4=0)+AND(A3=0,A2>=0)+AND(A3<0,A2=0,A4<=0))=1,(AND(A4>0,A5=0)+AND(A4=0,A3>=0)+AND(A4<0,A3=0,A5<=0))=0,A3<0),1,0),1,"")
 
Upvote 0
May I know how about pattern ( - 0 + )?
Try:
VBA Code:
Sub a1159856b()
'https://www.mrexcel.com/board/threads/identify-number-pattern-of-positive-zero-0-negative.1159856/

Dim i As Long, j As Long, n As Long
Dim va, z
Dim x As Double

n = Range("B" & Rows.Count).End(xlUp).Row + 1
va = Range("B1:B" & n)
Range("C2:C" & n).ClearContents
ReDim z(1 To 3)

For i = 2 To UBound(va, 1) - 1
    x = va(i, 1)
    
    Select Case x
        Case Is < 0
           If va(i + 1, 1) = 0 Then z(1) = x
        Case 0
           If va(i + 1, 1) > 0 And Len(z(1)) <> 0 Then z(2) = 0
        Case Is > 0
           If va(i - 1, 1) = 0 And Len(z(1)) <> 0 Then z(3) = x Else z(1) = Empty: z(2) = Empty
    End Select

    If Len(z(1)) <> 0 And Len(z(2)) <> 0 And Len(z(3)) <> 0 Then
        Cells(i, "C") = 1
        z(1) = Empty: z(2) = Empty: z(3) = Empty
    End If

Next
End Sub

Book2
BC
1Diff
20
30.4
40.5
5-2.3
60
70
80
951
10-0.1
110
120.41
130
14-0.5
150
16-0.2
Sheet2
 
Upvote 0
VBA is working and fast
Either is probably fast enough, but by my testing this (for the initial question) gives the same results about 7x faster.

VBA Code:
Sub PlusZeroMinus()
  Dim a As Variant, b As Variant
  Dim i As Long
  Dim bStart As Boolean, bCont As Boolean

  a = Range("B2", Range("B" & Rows.Count).End(xlUp)).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("C2").Resize(UBound(b)).Value = b
End Sub
 
Upvote 0
Solution
Thanks @Peter_SSs

For Number Pattern of ( - 0 + ), just change this part, right?

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
 
Upvote 0
just change this part, right?
Yes, just two characters need changing
Rich (BB code):
  Dim a As Variant, b As Variant
  Dim i As Long
  Dim bStart As Boolean, bCont As Boolean

  a = Range("B2", Range("B" & Rows.Count).End(xlUp)).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("C2").Resize(UBound(b)).Value = b
 
Upvote 0
Sorry to bump this thread,

If you have a time, please help me on pattern +0+ & -0- . I thought this both pattern do not important in my case reasearch, but they do a little impact.
-0+ +0-.xlsb
ABC
1DiffPlus Zero Plus
20
30.4
40.5
52.3
60
70
80
9-3.9
10-0.1
110
120.4
130
14-0.5
150
16-0.2
170.3
180
190
200
210
220.31
23-0.5
240
25-0.6
260.9
270.1
280
290
30-0.1
31-0.1
320.4
Sheet9

-0+ +0-.xlsb
ABC
1DiffMinus Zero Minus
20
30.4
40.5
52.3
60
70
80
9-3.9
10-0.1
110
120.4
130
14-0.5
150
16-0.21
170.3
180
190
200
210
220.3
23-0.5
240
25-0.61
260.9
270.1
280
290
30-0.1
31-0.1
320.4
Sheet9
I tried change sign (>0,>0 & <0,<0) VBA in Post #6 and work around VBA in #Post 5 but failed ?
 
Upvote 0

Forum statistics

Threads
1,215,039
Messages
6,122,799
Members
449,095
Latest member
m_smith_solihull

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