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:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
one way

Code:
Sub CountDistance()
Application.ScreenUpdating = False
Dim rng As Range, r As Long, c As Long, c1 As Range, c2 As Range, myStr As String, theCount As Integer
Set rng = Range("C5").CurrentRegion
For r = 2 To rng.Rows.Count
    For c = 1 To rng.Columns.Count
        Select Case c1 Is Nothing
            Case True: If rng(r, c) = 1 Then Set c1 = rng(r, c)
            Case Else: If rng(r, c) = 2 And rng(r, c + 1) <> 2 Then Set c2 = rng(r, c)
        End Select

        Select Case c2 Is Nothing
            Case False
                theCount = c2.Column - c1.Column + 1
                If myStr = "" Then myStr = theCount Else myStr = myStr & "," & theCount
                Set c1 = Nothing: Set c2 = Nothing
        End Select
    Next c
    
    Range("S" & rng(r, c).Row) = myStr: myStr = ""
    Set c1 = Nothing: Set c2 = Nothing
Next r

Range("S" & rng(2, 1).Row).Resize(rng.Rows.Count - 1).TextToColumns Destination:=Range("S6"), DataType:=xlDelimited, Comma:=True
End Sub
 
Last edited:
Upvote 0
one way

Code:
Sub CountDistance()
Application.ScreenUpdating = False
Dim rng As Range, r As Long, c As Long, c1 As Range, c2 As Range, myStr As String, theCount As Integer
Set rng = Range("C5").CurrentRegion
For r = 2 To rng.Rows.Count
    For c = 1 To rng.Columns.Count
        Select Case c1 Is Nothing
            Case True: If rng(r, c) = 1 Then Set c1 = rng(r, c)
            Case Else: If rng(r, c) = 2 And rng(r, c + 1) <> 2 Then Set c2 = rng(r, c)
        End Select

        Select Case c2 Is Nothing
            Case False
                theCount = c2.Column - c1.Column + 1
                If myStr = "" Then myStr = theCount Else myStr = myStr & "," & theCount
                Set c1 = Nothing: Set c2 = Nothing
        End Select
    Next c
    
    Range("S" & rng(r, c).Row) = myStr: myStr = ""
    Set c1 = Nothing: Set c2 = Nothing
Next r

Range("S" & rng(2, 1).Row).Resize(rng.Rows.Count - 1).TextToColumns Destination:=Range("S6"), DataType:=xlDelimited, Comma:=True
End Sub
Thank you Yongle, for your kind help!! Yes your code is resulting as request it is just perfect!!</SPAN></SPAN>

Kind Regards,
</SPAN></SPAN>
Kishan :)
</SPAN></SPAN>
 
Upvote 0
Using Excel 2000</SPAN></SPAN>

Hi,
</SPAN></SPAN>

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

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

Regards,
</SPAN>
Kishan
</SPAN></SPAN>

The principal request is solved, need help for the optional request if could be

Optional request: if possible could be highlighted columns C:P as show in post#1

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

Regards,
</SPAN>
Kishan


</SPAN>
 
Last edited:
Upvote 0
Another option for Numbers and colours !!!
Code:
[COLOR="Navy"]Sub[/COLOR] MG25Aug42
[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]
            col = col + 1
            Dn.Offset(, col) = 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
 
Upvote 0
Another option for Numbers and colours !!!
Code:
[COLOR=navy]Sub[/COLOR] MG25Aug42
[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]
            col = col + 1
            Dn.Offset(, col) = 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
Excellent!! Mick, for giving a 2 in 1 solution, I liked!! It very much :cool:</SPAN></SPAN>

Kind Regards,
</SPAN></SPAN>
Kishan :)
</SPAN></SPAN>
 
Upvote 0
Please Mick, may I ask, could you make a small piece of a modification? </SPAN></SPAN>

Count results in the columns S:AF could be placed by there 1's position find in the column C:P
</SPAN></SPAN>

For example: row 6 first 1&2-count start in D6 in position 2, so result in T6,
</SPAN></SPAN>
Second 1&2-count starts in F6 in position 4, so result in V6,
</SPAN></SPAN>
Third 1&2-count starts in J6 in position 4, so result in Z6,
</SPAN></SPAN>
Firth 1&2-count starts in N6 in position 4, so result in AD6,
</SPAN></SPAN>

Result 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
61
62
Sheet10


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

Kind Regards,
</SPAN></SPAN>
Kishan
</SPAN></SPAN>
 
Last edited:
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,857
Members
449,051
Latest member
excelquestion515

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