Count maximum to minimum consecutives

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 data in column C:P, and need results in columns T:Z

1-In the column T, I need first max consecutive count of "X's"
2-In the column U, I need second max consecutive count of "X's"
3- and continue third, fourth max consecutive in to columns V through Z

Example data.


Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAA
1
2
3
4
5P1P2P3P4P5P6P7P8P9P10P11P12P13P14Total XXXXXXXX
6X1212XX112X1124211
71X2221XX22XX115221
81X11XX22XX11XX72221
91X1221X11X12213111
10X1X1X11X111XX162111
11XXX1111X1X212X63111
122X1112XXX11121431
13X1XX1111111X114211
141XX12121112X2X4211
15X2X11X1111X11X511111
16111111XXX1112X431
171212X112X2111X3111
181XX112XX1X11X162211
191XX11211221X11321
20111X1111X11111211
21X12X1112X112XX52111
22111X1XX1X111114211
2311111XX211111222
2411111111112XX122
2512211111111X1111
2611X1111111112111
27XXX111X112X1115311
28X211211111XX1X4211
291X221112XX111X4211
3021XX11XX1X11115221
311X12211112111X211
32X11111212XX1XX5221
332XX11X11XXXX1X84211
3411111XX1X2XXXX7421
351111X1XXX22121431
361XX111X11X1X2X621111
371XX1XX121XX11X72221
38X1XXX111XXXXXX10631
391XXXXX11X12111651
40XXX1XX111XX1217322
412XX111XXX1XXXX9432
42X21X1X11X1X2X16111111
43X111122XX21XX15221
441XXXXXXXX11X21981
4511XX121XXXXXX1862
46X1X1X1X2XXXX1X9411111
472XX1X1X1XXXX1284211
48X1X2X1X1XX12XX8221111
49XX1XX2XXXX1XX1104222
50111XX1XX1X2XXX83221
51XX12112XXXXXXX972
52211XXXXX1XXXXX1055
5321111X1XX1XXX26321
541XX21X21XXXX2X84211
5512XX12X11XXXXX8521
5612X1X1XX1X11XX722111
572X212XX122XXXX7421
58
59
60
Sheet1


Thank you in advance

Regards,
Kishan
 
Last edited:

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG26May42
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Dc [COLOR="Navy"]As[/COLOR] Range, nRng [COLOR="Navy"]As[/COLOR] Range, Txt [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Num [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] R [COLOR="Navy"]As[/COLOR] Range, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Sp [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Set[/COLOR] Rng = Range("C6:P57")
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng.Rows
 Ac = 19: c = 0
    [COLOR="Navy"]Set[/COLOR] nRng = Nothing
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dc [COLOR="Navy"]In[/COLOR] Dn.Cells
        [COLOR="Navy"]If[/COLOR] Dc.Value = "X" [COLOR="Navy"]Then[/COLOR]
            [COLOR="Navy"]If[/COLOR] nRng [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR] [COLOR="Navy"]Set[/COLOR] nRng = Dc Else [COLOR="Navy"]Set[/COLOR] nRng = Union(nRng, Dc)
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] Dc
ReDim ray(1 To nRng.Areas.Count)
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] R [COLOR="Navy"]In[/COLOR] nRng.Areas
       c = c + 1
        ray(c) = R.Count
        Num = Num + R.Count
    [COLOR="Navy"]Next[/COLOR] R
    
    [COLOR="Navy"]For[/COLOR] c = 1 To UBound(ray)
         Ac = Ac + 1
         Cells(Dn.Row, Ac) = Application.Large(ray, c)
    [COLOR="Navy"]Next[/COLOR] c
    Cells(Dn.Row, 19) = Num: Num = 0
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Try this:-
Code:
[COLOR=navy]Sub[/COLOR] MG26May42
[COLOR=navy]Dim[/COLOR] Rng [COLOR=navy]As[/COLOR] Range, Dn [COLOR=navy]As[/COLOR] Range, Dc [COLOR=navy]As[/COLOR] Range, nRng [COLOR=navy]As[/COLOR] Range, Txt [COLOR=navy]As[/COLOR] [COLOR=navy]String[/COLOR]
[COLOR=navy]Dim[/COLOR] Ac [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] Num [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] R [COLOR=navy]As[/COLOR] Range, c [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] Sp [COLOR=navy]As[/COLOR] Variant
[COLOR=navy]Set[/COLOR] Rng = Range("C6:P57")
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng.Rows
 Ac = 19: c = 0
    [COLOR=navy]Set[/COLOR] nRng = Nothing
    [COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] Dc [COLOR=navy]In[/COLOR] Dn.Cells
        [COLOR=navy]If[/COLOR] Dc.Value = "X" [COLOR=navy]Then[/COLOR]
            [COLOR=navy]If[/COLOR] nRng [COLOR=navy]Is[/COLOR] Nothing [COLOR=navy]Then[/COLOR] [COLOR=navy]Set[/COLOR] nRng = Dc Else [COLOR=navy]Set[/COLOR] nRng = Union(nRng, Dc)
        [COLOR=navy]End[/COLOR] If
    [COLOR=navy]Next[/COLOR] Dc
ReDim ray(1 To nRng.Areas.Count)
    [COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] R [COLOR=navy]In[/COLOR] nRng.Areas
       c = c + 1
        ray(c) = R.Count
        Num = Num + R.Count
    [COLOR=navy]Next[/COLOR] R
    
    [COLOR=navy]For[/COLOR] c = 1 To UBound(ray)
         Ac = Ac + 1
         Cells(Dn.Row, Ac) = Application.Large(ray, c)
    [COLOR=navy]Next[/COLOR] c
    Cells(Dn.Row, 19) = Num: Num = 0
[COLOR=navy]Next[/COLOR] Dn
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
Mick :pray:, code is absolutely prefect!!

Thank you for your kind help

Have a nice day

Good Luck

Kind Regards,
Kishan :)
 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,007
Members
452,374
Latest member
keccles

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