Count consecutive after each break of following alphabet

Kishan

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

Hi,

I have got 1st set of 3 alphabets "ABC" in the range G2:J4 "that is manually changeable" for current "ABC" I want to look into column C and count consecutive A, B, C & D after each break and put them into G7 Below A consecutive, H7 Below B consecutive, I7 Below C consecutive, J7 Below D consecutive as shown below


I have got 2nd set of 3 alphabets "ABD" in the range L2:O4 "that is manually changeable" for current "ABD" I want to look into column D and count consecutive A, B, C & D after each break and put them into L7 Below A consecutive, M7 Below B consecutive, N7 Below C consecutive, O7 Below D consecutive as shown below

Please I want macro solution if possible

Sheet example...


Book1
ABCDEFGHIJKLMNOP
1
2AAAAAAAA
3BBBBBBBB
4CCCCDDDD
51211112222
6ABCDABCD
7CA2111
8AB2131
9AD2112
10BC1
11CB
12AA
13BA
14CB
15AD
16BD
17CC
18BA
19AA
20AB
21BD
22CC
23AA
24AA
25AA
26DA
27CA
28AB
29BD
30CC
31AA
32BB
33CD
34BC
35CA
36BB
37CC
38AC
39AA
40BB
41CD
42AD
43BD
44CD
45AC
46BA
47CB
48BD
49BC
50DA
51DA
52DA
53DA
54DA
55DB
56CD
57BD
58CC
59AA
60BA
61DA
62DA
63CA
64AB
65AD
66BD
67DD
68CC
69BA
70CA
71AB
72AC
73AB
74AC
75AB
76AD
77BD
78CC
79AA
80
Sheet1


Thank you in advance

Regards,
Kishan
 
Last edited:

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Please If not VBA, could be the formula solution
 
Upvote 0
any solution by making a helper column may be?
 
Upvote 0
Try this:-
It a bit long winded, because I was losing the will to live!!!
Code:
[COLOR="Navy"]Sub[/COLOR] MG28Nov28
Private [COLOR="Navy"]Sub[/COLOR] CommandButton1_Click()
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Txt [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] nTxt [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] K [COLOR="Navy"]As[/COLOR] Variant, R [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range("C7", Range("C" & Rows.Count).End(xlUp))
Range("G7:O500").ClearContents
Txt = Join(Application.Transpose(Rng(1).Offset(-5, 4).Resize(3)), "")
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
nTxt = Dn.Value & Dn.Offset(1).Value & Dn.Offset(2).Value
[COLOR="Navy"]If[/COLOR] nTxt = Txt [COLOR="Navy"]Then[/COLOR]
    [COLOR="Navy"]If[/COLOR] Not .Exists(nTxt) [COLOR="Navy"]Then[/COLOR]
        .Add nTxt, Dn.Offset(3)
    [COLOR="Navy"]Else[/COLOR]
        [COLOR="Navy"]Set[/COLOR] .Item(nTxt) = Union(.Item(nTxt), Dn.Offset(3))
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Acc [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] temp [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] Aph [COLOR="Navy"]As[/COLOR] Range, ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Rw [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] p [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] nStr [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] Num [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Sp [COLOR="Navy"]As[/COLOR] Variant
 [COLOR="Navy"]Set[/COLOR] Rng = Range("G6:j6")
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
p = 2
c = 7
ac = ac + 1
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] K [COLOR="Navy"]In[/COLOR] .keys
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] R [COLOR="Navy"]In[/COLOR] .Item(K)
    [COLOR="Navy"]If[/COLOR] Not R = Dn [COLOR="Navy"]Then[/COLOR]
        [COLOR="Navy"]If[/COLOR] p = 0 [COLOR="Navy"]Then[/COLOR]
            c = c + 1
            p = 2
         [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]ElseIf[/COLOR] R = Dn [COLOR="Navy"]Then[/COLOR]
        Cells(c, R.Column).Offset(, 3 + ac) = Cells(c, R.Column).Offset(, 3 + ac) + 1
        p = 0
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] R
[COLOR="Navy"]Next[/COLOR] K
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] With
Call nSub2
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
[COLOR="Navy"]Sub[/COLOR] nSub2()
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Txt [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] nTxt [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] K [COLOR="Navy"]As[/COLOR] Variant, R [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range("D7", Range("D" & Rows.Count).End(xlUp))
Txt = Join(Application.Transpose(Rng(1).Offset(-5, 8).Resize(3)), "")
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
nTxt = Dn.Value & Dn.Offset(1).Value & Dn.Offset(2).Value
[COLOR="Navy"]If[/COLOR] nTxt = Txt [COLOR="Navy"]Then[/COLOR]
    [COLOR="Navy"]If[/COLOR] Not .Exists(nTxt) [COLOR="Navy"]Then[/COLOR]
        .Add nTxt, Dn.Offset(3)
    [COLOR="Navy"]Else[/COLOR]
        [COLOR="Navy"]Set[/COLOR] .Item(nTxt) = Union(.Item(nTxt), Dn.Offset(3))
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Acc [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] temp [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] Aph [COLOR="Navy"]As[/COLOR] Range, ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Rw [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] p [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] nStr [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] Num [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Sp [COLOR="Navy"]As[/COLOR] Variant
 [COLOR="Navy"]Set[/COLOR] Rng = Range("L6:O6")
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
p = 2
c = 7
ac = ac + 1
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] K [COLOR="Navy"]In[/COLOR] .keys
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] R [COLOR="Navy"]In[/COLOR] .Item(K)
[COLOR="Navy"]If[/COLOR] Not R = Dn [COLOR="Navy"]Then[/COLOR]
    [COLOR="Navy"]If[/COLOR] p = 0 [COLOR="Navy"]Then[/COLOR]
        
        c = c + 1
        Num = 0
        p = 2
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]ElseIf[/COLOR] R = Dn [COLOR="Navy"]Then[/COLOR]
Cells(c, R.Column).Offset(, 7 + ac) = Cells(c, R.Column).Offset(, 7 + ac) + 1
p = 0
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] R
[COLOR="Navy"]Next[/COLOR] K
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Try this:-
It a bit long winded, because I was losing the will to live!!!
Regards Mick
Thanks a lot Mick, Awesome!! Now I can breath, it is presenting results, as per opening post request for me your input is vary valuable

I do appreciate for your time and solving my request - Thank you so much

Good Luck to you and have a nice week ahead

Regards,
Kishan :)

 
Last edited:
Upvote 0
Another.
Also long ;) (a macro and two functions)

Code:
Sub aTest()
    Dim s1 As String, rData As Range, rCrit As Range
    Dim arr As Variant, rCell As Range, i As Long
    
    Set rCrit = Range("G2:O5")
    Set rData = Range("C7:D" & Cells(Rows.Count, "C").End(xlUp).Row)
    
    For i = 1 To rCrit.Columns.Count Step 5
        s1 = MakeStr(rCrit.Columns(i), rData, rCrit.Columns(i).Cells(4))
        
        For Each rCell In rCrit.Columns(i).Cells(5).Resize(, 4)
            If InStr(s1, rCell.Value) Then
                arr = Consec(s1, rCell.Value)
                rCell.Offset(1).Resize(UBound(arr) + 1) = Application.Transpose(arr)
            End If
        Next rCell
    Next i
End Sub

Function MakeStr(rCrit As Range, r As Range, i As Long)
        Dim j As Long, strAux As String
        
        j = 1
        With r.Columns(i)
            Do
                If .Cells(j) = rCrit.Cells(1) _
                        And .Cells(j + 1) = rCrit.Cells(2) _
                        And .Cells(j + 2) = rCrit.Cells(3) Then
                    strAux = strAux & "," & .Cells(j + 3)
                    j = j + 3
                Else
                    j = j + 1
                End If
            Loop Until j > r.Rows.Count - 2
        End With
        MakeStr = Mid(strAux, 2)
End Function

Function Consec(s As String, crit As String)
    Dim spl As Variant, i As Long
    Dim lCounter As Long, strAux As String
    
    spl = Split(s, ",")
    For i = LBound(spl) To UBound(spl)
        If spl(i) = crit Then
            lCounter = lCounter + 1
        Else
            If lCounter > 0 Then
                strAux = strAux & " " & lCounter
                lCounter = 0
            End If
        End If
    Next i
    If spl(UBound(spl)) = crit Then strAux = strAux & " " & lCounter
    Consec = Split(Trim(strAux))
End Function

M.
 
Upvote 0
Another.
Also long ;) (a macro and two functions)

Code:
Sub aTest()
    Dim s1 As String, rData As Range, rCrit As Range
    Dim arr As Variant, rCell As Range, i As Long
    
    Set rCrit = Range("G2:O5")
    Set rData = Range("C7:D" & Cells(Rows.Count, "C").End(xlUp).Row)
    
    For i = 1 To rCrit.Columns.Count Step 5
        s1 = MakeStr(rCrit.Columns(i), rData, rCrit.Columns(i).Cells(4))
        
        For Each rCell In rCrit.Columns(i).Cells(5).Resize(, 4)
            If InStr(s1, rCell.Value) Then
                arr = Consec(s1, rCell.Value)
                rCell.Offset(1).Resize(UBound(arr) + 1) = Application.Transpose(arr)
            End If
        Next rCell
    Next i
End Sub

Function MakeStr(rCrit As Range, r As Range, i As Long)
        Dim j As Long, strAux As String
        
        j = 1
        With r.Columns(i)
            Do
                If .Cells(j) = rCrit.Cells(1) _
                        And .Cells(j + 1) = rCrit.Cells(2) _
                        And .Cells(j + 2) = rCrit.Cells(3) Then
                    strAux = strAux & "," & .Cells(j + 3)
                    j = j + 3
                Else
                    j = j + 1
                End If
            Loop Until j > r.Rows.Count - 2
        End With
        MakeStr = Mid(strAux, 2)
End Function

Function Consec(s As String, crit As String)
    Dim spl As Variant, i As Long
    Dim lCounter As Long, strAux As String
    
    spl = Split(s, ",")
    For i = LBound(spl) To UBound(spl)
        If spl(i) = crit Then
            lCounter = lCounter + 1
        Else
            If lCounter > 0 Then
                strAux = strAux & " " & lCounter
                lCounter = 0
            End If
        End If
    Next i
    If spl(UBound(spl)) = crit Then strAux = strAux & " " & lCounter
    Consec = Split(Trim(strAux))
End Function

M.
Hi Marcelo Branco, after checking with different data I found Mick macro was counting overlaps, so as I wanted to reply him I saw your code and went back to try it with same data and found your macro is giving the perfect results.

At the same time I thought to try extending 14 columns through C:P and shifting results to columns S:CI, really it has been so easy to work with just changing the ranges are shown in Red Amazing!! So far I will go with your macro "Set rCrit = Range
("S2:CI5")" & "Set rData = Range("C7:P" & Cells(Rows.Count, "C").End(xlUp).Row)"

Below example showing results by your macro, which are the correct.


Book1
ABCDEFGHIJKLMNOP
1
2BBBBCCCC
3CCCCAAAA
4BBBBBBBB
51211112222
6ABCDABCD
7DB2115
8DC21
9DA
10CB
11BD
12DC
13CB
14AC
15AB
16BC
17CA
18AA
19BA
20CA
21AA
22BA
23CA
24BA
25CA
26BA
27CB
28AC
29AA
30AA
31AA
32AB
33BD
34CD
35BC
36CB
37BC
38DB
39DC
40CA
41BB
42DD
43DD
44DD
45CC
46AA
47BB
48CD
49BC
50DA
51DA
52DA
53CB
54BC
55CA
56BB
57CD
58AD
59AD
60BD
61DC
62CB
63AC
64AA
65AB
66BD
67CC
68BB
69CC
70AB
71BC
72CA
73AA
74AB
75AC
76AB
77BC
78CA
79BB
80DC
81DB
82
Sheet5


I do appreciate a lot for your time and help

Thank you so much

Good Luck to you

Have a nice week ahead

Regards,
Kishan :)
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,431
Members
448,961
Latest member
nzskater

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