Separate database and count successive in string

Kishan

Well-known Member
Joined
Mar 15, 2011
Messages
1,648
Office Version
  1. 2010
Platform
  1. Windows
Using Excel 2000</SPAN></SPAN>

Hi,
</SPAN></SPAN>

Database Columns
</SPAN> C:P</SPAN>
Count successive ( 1’s ) in columns R:AE (count successive as in order appears ) Row3 1’s result 3-1-1-2-1
</SPAN></SPAN>
Count successive ( X’s + 2’s ) in columns AG:AT (count successive as in order appears ) Row3 X’s & 2’s result 1-3-2
</SPAN></SPAN>


Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASAT
1P1P2P3P4P5P6P7P8P9P10P11P12P13P14
2P1P2P3P4P5P6P7P8P9P10P11P12P13P14
3P1P2P3P4P5P6P7P8P9P10P11P12P13P14
4P1P2P3P4P5P6P7P8P9P10P11P12P13P14
5P1P2P3P4P5P6P7P8P9P10P11P12P13P1411111111111111X2X2X2X2X2X2X2
6X11121212X11X131121132
7XX21XX1121X2111212212111
8X11121212X11X1121121132
9211X21X112211111123011112
101X1X11211XXX1111222213
11X2XX2X1121111224112112
12X112111211X1X111321122
131XX21X111211X11132121111
141112XXX1XX1XXX311018
151XX11111X112X11251311
16X1X212XXX111XX113225
1711X121X11121X121131111111
1811111X11122X11532121
19X11XX1111211XX242312
20111121XX12X1X24111012121
211112211X1211X13212102111
2221X11X1112111212330122
231111121121XXX15211023
24X11211211X111X22221122
251111X111111111491
26X1111211XX11X14221113
Sheet1


Thank you in advance
</SPAN></SPAN>

Regards,
</SPAN></SPAN>
Kishan
</SPAN></SPAN>
 
Here the code with requested amendments:
Code:
Sub kishan2()
Dim range1 As Range
Dim range1b As Range
Dim range2 As Range
Dim range3 As Range
Dim lastrow As Long
Dim c As Range
Dim d As Range
Dim alfa As String
Dim alfa1 As String
Dim alfaX2 As String
Dim Pattern1
Dim PatternX2
Dim t As Long
Dim off As Long

lastrow = ActiveSheet.Cells(6, 3).End(xlDown).Row
Set range1 = Range(Cells(6, 3), Cells(6, 3).End(xlDown))
Set range2 = ActiveSheet.Range(Range("R6"), Cells(lastrow, 31))
Set range3 = ActiveSheet.Range(Range("AG6"), Cells(lastrow, 46))
range2.ClearContents
range3.ClearContents
For Each c In range1
    Set range1b = Range(c, Cells(c.Row, 17))
    
    For Each d In range1b
        alfa = alfa & d.Value
    Next d
    
    alfa1 = Trim(Replace(Replace(alfa, 2, " "), "X", " "))
    
    Do Until InStr(1, alfa1, "  ") = 0
        alfa1 = Replace(alfa1, "  ", " ")
    Loop
    
    alfaX2 = Trim(Replace(alfa, 1, " "))
    Do Until InStr(1, alfaX2, "  ") = 0
        alfaX2 = Replace(alfaX2, "  ", " ")
    Loop
    alfaX2 = Replace(Replace(alfaX2, "2X", "2 X"), "X2", "X 2")
    Pattern1 = Split(alfa1, " ")
    
    For t = 0 To UBound(Pattern1)
        Cells(c.Row, 18 + t).Value = Len(Pattern1(t))
    Next t
    
    PatternX2 = Split(alfaX2, " ")
    
    For t = 0 To UBound(PatternX2)
        If (t = 0 And Mid(PatternX2(t), 1, 1) = "2") Then 'Or (t = 0 And Mid(PatternX2(t), 1, 1) = "X") Then
            Cells(c.Row, 33 + t).Value = 0
            Cells(c.Row, 33 + t + 1).Value = Len(PatternX2(t))
            off = 1
        Else
            Cells(c.Row, 33 + t + off).Value = Len(PatternX2(t))
        End If
    Next t
    
    off = 0
    alfa = ""
    alfa1 = ""
    alfaX2 = ""
Next c
End Sub
Code gives results as in post #6 But I cannot guess why if X2 series begins with 2 in first output cell you want a 0 and with something like 21X1X it seems you want 0111 while I figured it was 01101. In other words a long series of single 2 or X take a place in report where you expected to count the other sign.

Hope it helps
 
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Here the code with requested amendments:
Code:
Sub kishan2()
End Sub
Code gives results as in post #6 But I cannot guess why if X2 series begins with 2 in first output cell you want a 0 and with something like 21X1X it seems you want 0111 while I figured it was 01101. In other words a long series of single 2 or X take a place in report where you expected to count the other sign.

Hope it helps
In the given example below I removed Step-1 (which was count consecutive 1’s) to explain better only X2 series and output 0 first if the series start with 2


Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASAT
1P1P2P3P4P5P6P7P8P9P10P11P12P13P14
2P1P2P3P4P5P6P7P8P9P10P11P12P13P14
3P1P2P3P4P5P6P7P8P9P10P11P12P13P14
4P1P2P3P4P5P6P7P8P9P10P11P12P13P14
5P1P2P3P4P5P6P7P8P9P10P11P12P13P14X2X2X2X2X2X2X2X2
621X1X1111111110120111
7XXXXXX112222226666
82222222221XXXX094094
91X2X1X2221XXX111233111133
101112XXX1XX1XXX01801323
111111121121XXX10230113
12X1X1X11X1211X1411111111
Step 2 & 3


Hi B___P, to workout steps 2 & 3 in conjunction correctly I have to decide in both steps 2 & 3 either series start with X2 or with 2X in my case I decided series for both steps 2 & 3 to be started with X2

For example Row6 series starts with 2
Result Step-1 is X=0, 2=1 & X=2………...……
0-1-2
Result Step-2 Is X=0, 2=1 & X, X=1-1………0-1-1-1
So output 0 in X column if start with 2 which balanced both steps and I can read clearly (X =0) (2 =1) & X = 2 but as result is 1-1 this mean X’s are separate

For example Row9 series starts with X
Result Step-1 is X=1, 2=1, X=2, 2=3, X=3……..………
1-1-2-3-3
Result Step-2 Is X=1, 2=1, X=1-1, 2=3, X=3….………1-1-1-1-3-3So output 1 in X column if start with X which balanced both steps and I can read clearly (X =1) (2 =1) (X = 1-1) (2=3) & (X=3) so this says 2nd time 2 X’s are appeared separate

For example Row12 series starts with X
Result Step-1 is X=4, 2=1, X=1……........………
4-1-1
Result Step-2 Is X=1-1-1-1, 2=1, X=1….………1-1-1-1-1-1So output 1 in X column if start with X which balanced both steps and I can read clearly (X =4) (2 =1) (X = 1) so this says 1st time 4 X’s are appeared separate

Hope I have explained well if not please let me know I will try again my best

Thank you for all your kind help and time

Regards,
Kishan :)
 
Upvote 0
I split my answer in 3 parts one for each step.
1st step: counting 1s
Code:
Sub kishan_step1()
Dim range1 As Range
Dim range1b As Range
Dim range2 As Range
Dim lastrow As Long
Dim c As Range
Dim d As Range
Dim alfa As String
Dim alfa1 As String
Dim Pattern1
Dim t As Long
Dim off As Long

lastrow = ActiveSheet.Cells(6, 3).End(xlDown).Row
Set range1 = Range(Cells(6, 3), Cells(6, 3).End(xlDown))
Set range2 = ActiveSheet.Range(Range("R6"), Cells(lastrow, 31))
range2.ClearContents
For Each c In range1
    Set range1b = Range(c, Cells(c.Row, 17))
    
    For Each d In range1b
        alfa = alfa & d.Value
    Next d
    
    alfa1 = Trim(Replace(Replace(alfa, 2, " "), "X", " "))
    
    Do Until InStr(1, alfa1, "  ") = 0
        alfa1 = Replace(alfa1, "  ", " ")
    Loop
    
    Pattern1 = Split(alfa1, " ")
    
    For t = 0 To UBound(Pattern1)
        Cells(c.Row, 18 + t).Value = Len(Pattern1(t))
    Next t
    
    alfa = ""
    alfa1 = ""
Next c
End Sub
This puts results starting from cell R6. Results are the same as my 1sp post.
 
Upvote 0
step2, counting Xs and 2s
Code:
Sub kishan_step2()
Dim range1 As Range
Dim range1b As Range
Dim range3 As Range
Dim lastrow As Long
Dim c As Range
Dim d As Range
Dim alfa As String
Dim alfaX2 As String
Dim PatternX2
Dim t As Long
Dim off As Long

lastrow = ActiveSheet.Cells(6, 3).End(xlDown).Row
Set range1 = Range(Cells(6, 3), Cells(6, 3).End(xlDown))
Set range3 = ActiveSheet.Range(Range("AG6"), Cells(lastrow, 46))

range3.ClearContents
For Each c In range1
    Set range1b = Range(c, Cells(c.Row, 17))
    
    For Each d In range1b
        alfa = alfa & d.Value
    Next d
    
    alfaX2 = Trim(Replace(alfa, 1, " "))
    Do Until InStr(1, alfaX2, "  ") = 0
        alfaX2 = Replace(alfaX2, "  ", " ")
    Loop
    alfaX2 = Replace(Replace(alfaX2, "2X", "2 X"), "X2", "X 2")
    
    PatternX2 = Split(alfaX2, " ")
    
    For t = 0 To UBound(PatternX2)
        If (t = 0 And Mid(PatternX2(t), 1, 1) = "2") Then
            Cells(c.Row, 33 + t).Value = 0
            Cells(c.Row, 33 + t + 1).Value = Len(PatternX2(t))
            off = 1
        Else
            Cells(c.Row, 33 + t + off).Value = Len(PatternX2(t))
        End If
    Next t
    
    off = 0
    alfa = ""
    alfaX2 = ""
Next c
End Sub
This is a kind of helper step as you stated in your first post but after all it reveals to be useful for some evaluations. Finally I got what you meant. I keep treating it as an helper step so results start at cell AG6
 
Upvote 0
And here the 3rd step
Code:
Sub kishan_step3()
Dim range1 As Range
Dim range1b As Range
Dim range4 As Range
Dim lastrow As Long
Dim c As Range
Dim d As Range
Dim alfa As String
Dim alfaX2 As String
Dim PatternX2
Dim t As Long
Dim off As Long

lastrow = ActiveSheet.Cells(6, 3).End(xlDown).Row
Set range1 = Range(Cells(6, 3), Cells(6, 3).End(xlDown))
Set range4 = ActiveSheet.Range(Range("Av6"), Cells(lastrow, 61))
range4.ClearContents
For Each c In range1
    Set range1b = Range(c, Cells(c.Row, 17))
    
    For Each d In range1b
        alfa = alfa & d.Value
    Next d
    
    alfaX2 = Trim(Replace(alfa, 1, ""))
    alfaX2 = Replace(Replace(alfaX2, "2X", "2 X"), "X2", "X 2")
    PatternX2 = Split(alfaX2, " ")
    
    For t = 0 To UBound(PatternX2)
        If (t = 0 And Mid(PatternX2(t), 1, 1) = "2") Then
            Cells(c.Row, 48 + t).Value = 0
            Cells(c.Row, 48 + t + 1).Value = Len(PatternX2(t))
            off = 1
        Else
            Cells(c.Row, 48 + t + off).Value = Len(PatternX2(t))
        End If
    Next t
    
    off = 0
    alfa = ""
    alfaX2 = ""
Next c
End Sub
It aggregates Xs and 2s not interrupted by the other sign. Results start at cell AV6 and are the same as my first post.

I hope the 3 steps comply with your needs and help you. I don't know from which country you are writing so I wish you a nice rest of the day.
 
Upvote 0
I split my answer in 3 parts one for each step.
1st step: counting 1s
Code:
[COLOR=#000000]Sub kishan_[B]step1[/B]()
End Sub
[/COLOR]
This puts results starting from cell R6. Results are the same as my 1sp post.

step2, counting Xs and 2s
Code:
[COLOR=#000000]Sub kishan_[B]step2[/B]()
End Sub[/COLOR]
This is a kind of helper step as you stated in your first post but after all it reveals to be useful for some evaluations. Finally I got what you meant. I keep treating it as an helper step so results start at cell AG6

And here the 3rd step
Code:
[COLOR=#000000]Sub kishan_[B]step3[/B]()
End Sub
[/COLOR]
It aggregates Xs and 2s not interrupted by the other sign. Results start at cell AV6 and are the same as my first post.

I hope the 3 steps comply with your needs and help you. I wish you a nice rest of the day.

Magnificent!! B___P, Everyone says, "Questions make the road to reach a target." But I believe it is more important to get a good answer for your Question.

Building 3 macros in to 3-separate steps that help me a lot

Thank you very much for giving best answers, understanding my viewpoint.

I really appreciate your time.

Have a good weekend

Kind Regards,
Kishan :)
 
Upvote 0

Forum statistics

Threads
1,215,294
Messages
6,124,101
Members
449,142
Latest member
championbowler

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