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>
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
I'll try anything once. See if this works for you.
Code:
Sub t()
Dim i As Long, c As Range, cnt1 As Long, cnt2 As Long
With ActiveSheet
    For i = 6 To .Range("C" & Rows.Count).End(xlUp).Row
        cnt1 = 0
        cmt2 = 0
        For Each c In Range("C" & i, "P" & i)
            If c.Value = 1 Then
                cnt1 = cnt1 + 1
            ElseIf cnt1 > 0 And c.Value <> 1 Then
                If .Cells(i, 18).Value = "" Then
                    .Cells(i, 18) = cnt1
                    cnt1 = 0
                Else
                    .Cells(i, "AF").End(xlToLeft).Offset(, 1) = cnt1
                    cnt1 = 0
                End If
                cnt1 = 0
            End If
            If c.Value = "X" Or c.Value = 2 Then
                cnt2 = cnt2 + 1
            ElseIf cnt2 > 0 And c.Value <> "X" And c.Value <> 2 Then
                If .Cells(i, "AG").Value = "" Then
                    .Cells(i, "AG") = cnt2
                    cnt2 = 0
                Else
                    .Cells(i, "AU").End(xlToLeft).Offset(, 1) = cnt2
                    cnt2 = 0
                End If
            End If
        Next
    Next
End With
End Sub
 
Last edited:
Upvote 0
I'll try anything once.
Thank you JLGWhiz, for your help. Above phrase is lovely. :)
See if this works for you.
1st Result for 1’s code shows 3-1-1-2, which should be 3-1-1-2-1
2nd Result for (X’s + 2’s) is also not correct as per post#1

Any ways this gives me idea I must change question in 3 steps

Step-1, Count 1’s consecutive (example result Row6 = 3-1-1-2-1) result in column R:X

Step-2, This is bit awkward start counting 1st with X (example result Row6 = 1-1-1-1-1-1 all are singles no consecutives). And the (example result Row9 = 0-1-1-1-1-2, X not find so first = 0 then 4 singles 1-1-1-1, and then 2 are consecutives = 2)

Step-3, Result must be 1-3-2 this is also bit awkward (count till same sign continue). For example row 6 first X =1 time, then 2 = 3 times, then X=2 times so result (1-3-2)

Please here is the new layout


Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAO
1P1P2P3P4P5P6P7P8P9P10P11P12P13P14
2P1P2P3P4P5P6P7P8P9P10P11P12P13P14
3P1P2P3P4P5P6P7P8P9P10P11P12P13P14
4P1P2P3P4P5P6P7P8P9P10P11P12P13P14
5P1P2P3P4P5P6P7P8P9P10P11P12P13P141111111X2X2X2X2X
6X11121212X11X131121111111132
7XX21XX1121X2111212212111212111
8X11121212X11X1121121111111132
9211X21X112211111123011112011112
101X1X11211XXX11112221113213
11X2XX2X11211112241121111112112
12X112111211X1X12321111111122
131XX21X111211X1113212111121111
141112XXX1XX1XXX31101323018
151XX11111X112X112512111311
16X1X212XXX111XX113111132225
1711X121X11121X12113111111111111
1811111X11122X11532121121
19X11XX1111211XX2421212312
20111121XX12X1X241110121111012121
211112211X1211X1321210211102111
2221X11X1112111212330111110122
231111121121XXX152110113023
24X11211211X111X2222111111122
251111X1111111114911
26X1111211XX11X142211121113
Sheet2


Thank You

Regards,
Kishan
 
Upvote 0
Hi, Please ignore post#3 I think it is a bit messy. I will put all 3-steps separately in 3 different posts, here.

Sorry for inconvenience

Regards,
Kishan
 
Upvote 0
Hi, Step-1, Count 1’s consecutive (example result Row6 = 3-1-1-2-1) result in column R:AE


Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAE
1P1P2P3P4P5P6P7P8P9P10P11P12P13P14
2P1P2P3P4P5P6P7P8P9P10P11P12P13P14
3P1P2P3P4P5P6P7P8P9P10P11P12P13P14
4P1P2P3P4P5P6P7P8P9P10P11P12P13P14
5P1P2P3P4P5P6P7P8P9P10P11P12P13P1411111111111111
6X11121212X11X131121
7XX21XX1121X2111212
8X11121212X11X1121121
9211X21X112211111123
101X1X11211XXX1111222
11X2XX2X1121111224
12X112111211X1X111321
131XX21X111211X111321
141112XXX1XX1XXX311
151XX11111X112X11251
16X1X212XXX111XX113
1711X121X11121X1211311
1811111X11122X11532
19X11XX1111211XX242
20111121XX12X1X24111
211112211X1211X132121
2221X11X111211121233
231111121121XXX15211
24X11211211X111X22221
251111X11111111149
26X1111211XX11X14221
Step-1
 
Upvote 0
Hi, Step-2, This is bit awkward start counting 1st with X (example result Row6 = 1-1-1-1-1-1 all are singles no consecutives). And the (example result Row9 = 0-1-1-1-1-2, X not find so first = 0 then 4 singles 1-1-1-1, and then 2 are consecutives = 2)


Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAE
1P1P2P3P4P5P6P7P8P9P10P11P12P13P14
2P1P2P3P4P5P6P7P8P9P10P11P12P13P14
3P1P2P3P4P5P6P7P8P9P10P11P12P13P14
4P1P2P3P4P5P6P7P8P9P10P11P12P13P14
5P1P2P3P4P5P6P7P8P9P10P11P12P13P14X2
6X11121212X11X1111111
7XX21XX1121X211212111
8X11121212X11X1111111
9211X21X1122111011112
101X1X11211XXX111113
11X2XX2X112111121121111
12X112111211X1X111111
131XX21X111211X121111
141112XXX1XX1XXX01323
151XX11111X112X12111
16X1X212XXX111XX111132
1711X121X11121X111111
1811111X11122X11121
19X11XX1111211XX1212
20111121XX12X1X20121111
211112211X1211X102111
2221X11X11121112011111
231111121121XXX10113
24X11211211X111X11111
251111X1111111111
26X1111211XX11X11121
Step-2
 
Upvote 0
Hi, Step-3, Result must be 1-3-2 this is also bit awkward (count till same sign continue). For example row6 first X =1 time, then 2 = 3 times, then X=2 times so result (1-3-2)

Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAE
1P1P2P3P4P5P6P7P8P9P10P11P12P13P14
2P1P2P3P4P5P6P7P8P9P10P11P12P13P14
3P1P2P3P4P5P6P7P8P9P10P11P12P13P14
4P1P2P3P4P5P6P7P8P9P10P11P12P13P14
5P1P2P3P4P5P6P7P8P9P10P11P12P13P14X2X2X2X2X2X2X2
6X11121212X11X1132
7XX21XX1121X211212111
8X11121212X11X1132
9211X21X1122111011112
101X1X11211XXX11213
11X2XX2X11211112112112
12X112111211X1X1122
131XX21X111211X121111
141112XXX1XX1XXX018
151XX11111X112X1311
16X1X212XXX111XX225
1711X121X11121X111111
1811111X11122X11121
19X11XX1111211XX312
20111121XX12X1X2012121
211112211X1211X102111
2221X11X111211120122
231111121121XXX1023
24X11211211X111X122
251111X1111111111
26X1111211XX11X1113
Step-3
 
Upvote 0
Hi, I think 3 codes 1 code for each step will be better.

Thank you in advance

Regards,
Kishan
 
Upvote 0
Hi Kishan,
you explanation is becoming a bit problematic.:eek:

I solved your question with one step code. Your second step looks like a helper step to achieve 3rd step and I think it is unnecessary.

Give this a try
Code:
Sub kishan()
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, ""))
    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

Hope this helps
 
Upvote 0
Hi Kishan,
you explanation is becoming a bit problematic.:eek:
Hi B___P, I totally agree with you when I reread my post I found it was not understandable even to me.

Hi Kishan,
I solved your question with one step code. Your second step looks like a helper step to achieve 3rd step and I think it is unnecessary.
Yes, what you are thinking is right, 2nd step was helpful to achieve the 3rd step.

But now I am thinking it can be useful. For example I am getting the results for (X’s + 2’s) say for the Row6 = (1-3-2) mean 1=X’s, 3=2’s & again 2=X’s (So step-2 if result is 1-1-1-1-1-1 this assign X’s & 2’s are separate)

For example the results for (X’s + 2’s) say for the Row10 = (2-1-3) mean 2=X’s, 1=2’s & again 3=X’s (So step-2 if result is 1-1-1-3 this assign all first X’s & 2’s are separate and last 3 X’s are consecutive)

So I am thinking if it is not much trouble for you can you make a separate macro for Step-2 (which places result at the end of macro Step-1) in columns AV:BI (separate will be good because it will give me an option and I can use it as necessary)

Hi Kishan,
Give this a try
Code:
Sub kishan()
End Sub

Hope this helps
Great!!
Thank you very much B___P, this is working flawless!!

I appreciate your help and time.

Kind Regards,
Kishan :)
 
Upvote 0

Forum statistics

Threads
1,214,568
Messages
6,120,278
Members
448,953
Latest member
Dutchie_1

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