Count the distance Between 1 and 2

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>

I want to count the distance between the character "1" and "2", count start should be with "1" and to last "2"
</SPAN></SPAN>

Data are in columns C:P, and the count results are shown in the columns S:AF
</SPAN></SPAN>

Note optional request: if possible could be highlighted columns C:P as shown
</SPAN></SPAN>

Result data example
</SPAN></SPAN>


Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAH
1
2
3
4
5C1C2C3C4C5C6C7C8C9C10C11C12C13C14C1C2C3C4C5C6C7C8C9C10C11C12C13C14
6X1212XX112X1122233
71X2221XX22XX1155
81X11XX22XX11XX8
91X1221X11X122158
10X1X1X11X111XX1
11XXX1111X1X212X82
122X1112XXX1112144
13X1XX1111111X11
141XX12121112X2X524
15X2X11X1111X11X
16111111XXX1112X13
171212X112X2111X223
181XX112XX1X11X16
191XX11211221X1164
20111X1111X11111
21X12X1112X112XX243
22111X1XX1X11111
2311111XX211111286
2411111111112XX111
2512211111111X113
2611X1111111112113
27XXX111X112X1118
28X211211111XX1X3
291X221112XX111X44
3021XX11XX1X1111
311X12211112111X55
3211X122211X1X1X7
3321X1X1X1111111
34X11111212XX1XX62
351211X111X2X1XX28
36X1111X1211X1XX7
372X111XX11X11X1
381X2X1X1111121X38
39X1XX1X11X1221111
401XX12X11X1X12157
411X11X1XX21111X9
42121111X1XX1X22212
43X111111121221X83
442XX11X11XXXX1X
45X211X11211112165
461XX1111111X11X
47XX111XX21111XX6
48111X1X1111X111
49XX11X111111111
50111X1111211X119
5111111XX1X2XXXX10
52111121XXX111115
532X11X11XX1111212
541XX111111212121022
551111X11XXX11X1
56X111X121112X1164
5711XX1X11XXX21112
58122111X21XXX1X35
5911X1121111X1X16
6012211X11111X1X3
6121X1X111122X2X10
6222211211112X2X35
63X1211X11212111232
64XXX1112211122155
65XX2XX2X122122X33
661111X11X212121922
67X1212212112X112323
68XX1222111X211X45
692X212XX122XXXX23
7022XX1X11111X1X
7121X1222221X21X83
7221211X112X12X1262
732X22111X1111X1
741XX12X12111221525
752X111X111X21119
762111XX1XX1112X12
77X2122X1122211135
78211X211X2X222X44
79X112X12111111X32
801212X21X12X111224
812XX1221112111X34
82111121112121XX542
831212111221211X2252
84
85
Sheet10


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

Regards,
</SPAN>
Kishan
</SPAN></SPAN>
 
Last edited:
Try this:-
Code:
[COLOR=navy]Sub[/COLOR] MG26Aug12
[COLOR=navy]Dim[/COLOR] Rng [COLOR=navy]As[/COLOR] Range, Dn [COLOR=navy]As[/COLOR] Range, ac [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] col [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] c [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] cls [COLOR=navy]As[/COLOR] [COLOR=navy]Long[/COLOR]
[COLOR=navy]Set[/COLOR] Rng = Range(Range("c6"), Range("c" & Rows.Count).End(xlUp))
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng
c = 0: col = 15
    [COLOR=navy]For[/COLOR] ac = 1 To 14
        [COLOR=navy]If[/COLOR] Dn(, ac) = 1 [COLOR=navy]Then[/COLOR] [COLOR=navy]If[/COLOR] c = 0 [COLOR=navy]Then[/COLOR] c = ac
        [COLOR=navy]If[/COLOR] c > 0 And Dn(, ac) = 2 And Not Dn(, ac + 1) = 2 [COLOR=navy]Then[/COLOR]
            Dn.Offset(, col + c) = ac - c + 1
            cls = IIf(cls = vbRed, vbGreen, vbRed)
            Dn.Offset(, c - 1).Resize(, ac - c + 1).Interior.Color = cls
            c = 0
        [COLOR=navy]End[/COLOR] If
    [COLOR=navy]Next[/COLOR] ac
[COLOR=navy]Next[/COLOR] Dn
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
Much Appreciate!! Mick, code is giving the results as request!!</SPAN></SPAN>

Thank you for your kind help. Making my life easier!!
</SPAN></SPAN>

Have a great weekend
</SPAN></SPAN>

Kind Regards,
</SPAN></SPAN>
Kishan :)
</SPAN></SPAN>
 
Last edited:
Upvote 0

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Forum statistics

Threads
1,223,706
Messages
6,173,998
Members
452,542
Latest member
Bricklin

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