Find Number Pattern in Data - need 1 Column solution

ibmy

Board Regular
Joined
Mar 4, 2020
Messages
134
Office Version
  1. 2016
Platform
  1. 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 :

4 no.xlsb
FIJ
1DataExpected ResultExplanation
2
30.3
4-0.1
50
60.91
7-0.7Pattern found. Assign "1" at Positive Value & Start new find at this row
8-1.7
9-1.1
10-1
110
120
130
140.21
15-1.8Pattern found. Assign "1" at Positive Value & Start new find at this row
160
170
1811
19-2Pattern found. Assign "1" at Positive Value & Start new find at this row
20-0.6
210
221.3
231.2
24-0.5
25-1.9
26-2
270.4
28-0.1
290
301.2
310
32-0.3
330
340
350.4
360
370.7
38-0.7
Sheet1


This is my current solution but need 2 columns :

4 no.xlsb
FGH
1DataCodeFunction IF-AND
2
30.3
4-0.1
50
60.911
7-0.7 
8-1.7 
9-1.1 
10-1 
110 
120 
130 
140.211
15-1.8 
160 
170 
18111
19-2 
20-0.6 
210 
221.31 
231.2 
24-0.5 
25-1.9 
26-2 
270.4 
28-0.1 
290 
301.21 
310 
32-0.3 
330 
340 
350.41 
360 
370.7 
38-0.7 
Sheet2
Cell Formulas
RangeFormula
H6:H38H6=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.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi @ibmy .
Thanks for posting on MrExcel.​
I hope you are well.​

Try this.
It does not require the auxiliary column. Results in column "I"
VBA Code:
Sub FindNumberPattern()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
  Dim itfound As Boolean
 
  a = Range("F2", Range("F" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a, 1) - 3
    If a(i, 1) < 0 And a(i + 1, 1) = 0 Then
      itfound = False
      For k = i + 2 To UBound(a) - 1
        If a(k, 1) > 0 And a(k + 1, 1) < 0 Then
          itfound = True
          b(k, 1) = 1
          Exit For
        ElseIf a(k, 1) = 0 Then
          'continue
        Else
          itfound = False
          Exit For
        End If
      Next
      If itfound = True Then i = k
    End If
  Next i
 
  Range("I2").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
Thanks expert, @DanteAmor
The code working great.

I manage to find opposite of its pattern with your code by changing > < > sign, + 0 - +

If you could help me one more thing with the data that consist of no negative value, all are positive value (absolute).
My interest number pattern is in range which is:

1. Starting a value greater than 0.5
2. Follow by at least a Zero ( 0 / 00 / 000 / 000..... )
3
. Follow by a value less than 0.6 and greater than 0
4. End with a valuer greater than 0.5

I managed to get result by changing > 0.5 , <0.6 > 0.5 :

VBA Code:
Sub Abs_Pattern()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
  Dim itfound As Boolean
 
  a = Range("F2", Range("F" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a, 1) - 3
    If a(i, 1) > 0.5 And a(i + 1, 1) = 0 Then
      itfound = False
      For k = i + 2 To UBound(a) - 1
        If a(k, 1) < 0.6 And a(k + 1, 1) > 0.5 Then
          itfound = True
          b(k, 1) = 1
          Exit For
        ElseIf a(k, 1) = 0 Then
          'continue
        Else
          itfound = False
          Exit For
        End If
      Next
      If itfound = True Then i = k
    End If
  Next i
 
  Range("I2").Resize(UBound(b)).Value = b
End Sub

But there is problem when I change this line a(k, 1) < 0.6 , it violate third rule ( 3. Follow by a value less than 0.6 and greater than 0 ) ,it includes 0.

I tried change the line to:
1) " If a(k, 1) = 0.1, 0.2, 0.3, 0.4, 0.5 "
2) " If 0.6 > a(k, 1) > 0 "

But none working.

range 4 no.xlsb
FI
1DataRange
2
30.6
40.8
50
60
70.21
80.9
90.1
101.6
110
120
130.51
141.1
150.7
160.7
170
1801
190.7
201.4
210.5
220.9
230
2401
250.7
260.2
Sheet14

Yellow Cell are wrong assing "1"
 
Upvote 0
1. Starting a value greater than 0.5
2. Follow by at least a Zero ( 0 / 00 / 000 / 000..... )
3
. Follow by a value less than 0.6 and greater than 0
4. End with a valuer greater than 0.5

Try this:

VBA Code:
Sub Abs_Pattern()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
  Dim itfound As Boolean
 
  a = Range("F2", Range("F" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a, 1) - 3
    If a(i, 1) > 0.5 And a(i + 1, 1) = 0 Then
      itfound = False
      For k = i + 2 To UBound(a) - 1
        If a(k, 1) < 0.6 And a(k, 1) > 0 And a(k + 1, 1) > 0.5 Then
          itfound = True
          b(k, 1) = 1
          Exit For
        ElseIf a(k, 1) = 0 Then
          'continue
        Else
          itfound = False
          Exit For
        End If
      Next
      If itfound = True Then i = k
    End If
  Next i
 
  Range("I2").Resize(UBound(b)).Value = b
End Sub
 
Upvote 0
If you are interested in alternatives, I think this would be one for the original question & data layout.

VBA Code:
Sub Find_Pattern()
  With Range("I5:I" & Range("F" & Rows.Count).End(xlUp).Row)
    .Formula = "=IF(AND(F5>0,F6<0,F4=0,IFERROR(AGGREGATE(14,6,ROW(F$3:F4)/(F$3:F4<0),1),0)>IFERROR(AGGREGATE(14,6,ROW(F$3:F4)/(F$3:F4>0),1),0)),1,"""")"
    .Value = .Value
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,135
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