Count "0" and "1" in a single cell separated by vertical bar

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>

Data in columns C:P, need to count "0" and "1" in a single cell separated by vertical bar in the column R
</SPAN></SPAN>

Example data
</SPAN></SPAN>


Book1
ABCDEFGHIJKLMNOPQRST
1
2
3
4
5P1P2P3P4P5P6P7P8P9P10P11P12P13P14EMCount 0 And 1
60000000000000014
7000100110100113 | 1 | 2 | 2 | 1 | 1 | 2 | 2
801110101001101u
9011001011001111 | 2 | 2 | 1 | 1 | 2 | 2 | 3
10101011011000111 | 1 | 1 | 2 | 1 | 2 | 3 | 2
11000000110111106 | 2 | 1 | 4 | 1
12011011101101011 | 2 | 1 | 3 | 1 | 2 | 1 | 1 | 1 | 1
13100010111011011 | 3 | 1 | 1 | 3 | 1 | 2 | 1 | 1
14111000010010003 | 4 | 1 | 2 | 1 | 3
15100111110110011 | 2 | 5 | 1 | 2 | 2
16010010000111001 | 1 | 2 | 1 | 4 | 3 | 2
17110101011101012 | 1 | 1 | 1 | 1 | 1 | 3 | 1 | 1 | 1 | 1
18111110111000115 | 1 | 3 | 3 | 2
19011001111011001 | 2 | 2 | 4 | 1 | 2 | 2
20111101001001004 | 1 | 1 | 2 | 1 | 2 | 1 | 2
21111001101011013 | 2 | 2 | 1 | 1 | 1 | 2 | 1 | 1
22010110111011101 | 1 | 1 | 2 | 1 | 3 | 1 | 3 | 1
23111110110100015 | 1 | 2 | 1 | 1 | 3 | 1
24011011011011101 | 2 | 1 | 2 | 1 | 2 | 1 | 3 | 1
25111101111111114 | 1 | 9
26011110110011011 | 4 | 1 | 2 | 2 | 2 | 1 | 1
27100000101110111 | 5 | 1 | 1 | 3 | 1 | 2
28111010000010103 | 1 | 1 | 5 | 1 | 1 | 1 | 1
29111001010101003 | 2 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 2
30111011101101103 | 1 | 3 | 1 | 2 | 1 | 2 | 1
31111011111111113 | 1 | 10
32101101100010011 | 1 | 2 | 1 | 2 | 3 | 1 | 2 | 1
33001101001001012 | 2 | 1 | 1 | 2 | 1 | 2 | 1 | 1 | 1
34101011111101011 | 1 | 1 | 1 | 6 | 1 | 1 | 1
35011100100110011 | 3 | 2 | 1 | 2 | 2 | 2 | 1
36100011001101001 | 3 | 2 | 2 | 2 | 1 | 1 | 2
37011111111000001 | 8 | 5
38001100111001112 | 2 | 2 | 3 | 2 | 3
39010101000001101 | 1 | 1 | 1 | 1 | 1 | 5 | 2 | 1
40111001001010013 | 2 | 1 | 2 | 1 | 1 | 1 | 2 | 1
41000110111100113 | 2 | 1 | 4 | 2 | 2
42000101011010013 | 1 | 1 | 1 | 1 | 2 | 1 | 1 | 2 | 1
43001001011111012 | 1 | 2 | 1 | 1 | 5 | 1 | 1
44001011110101102 | 1 | 1 | 4 | 1 | 1 | 1 | 2 | 1
45110110111110112 | 1 | 2 | 1 | 5 | 1 | 2
46111100011111014 | 3 | 5 | 1 | 1
47001111101111012 | 5 | 1 | 4 | 1 | 1
48101001011111111 | 1 | 1 | 2 | 1 | 1 | 7
49010101010000011 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 5 | 1
50011110111111101 | 4 | 1 | 7 | 1
51010110001000111 | 1 | 1 | 2 | 3 | 1 | 3 | 2
52011100111111001 | 3 | 2 | 6 | 2
53011000111110001 | 2 | 3 | 5 | 3
54010010001001001 | 1 | 2 | 1 | 3 | 1 | 2 | 1 | 2
55000101101101013 | 1 | 1 | 2 | 1 | 2 | 1 | 1 | 1 | 1
561111111111111114
57
58
Sheet1


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

Regards,
</SPAN>
Kishan
</SPAN></SPAN>
 
Last edited:
Add to line, as shown in Red:-
Code:
        nStr = nStr & IIf(nStr = "", c, "|" & c)
        c = 0
    End If
End If

Dn.Offset(, 15).Value =[COLOR="#FF0000"][B] Dn.Value & " - " & [/B][/COLOR]nStr

nStr = "": c = 0
Next Dn
End Sub
 
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Add to line, as shown in Red:-
Code:
        nStr = nStr & IIf(nStr = "", c, "|" & c)
        c = 0
    End If
End If

Dn.Offset(, 15).Value =[COLOR=#ff0000][B] Dn.Value & " - " & [/B][/COLOR]nStr

nStr = "": c = 0
Next Dn
End Sub
MickG, thanks a lot for modifying code and adding the first number from column C, this change has made a unique sequence and avoided all duplicates</SPAN></SPAN>

I am grateful to you for your help. Have a nice day
</SPAN></SPAN>
Kind Regards,
</SPAN></SPAN>
Kishan
</SPAN></SPAN>
 
Upvote 0
Here is another (more compact) macro that should also work...
Code:
[table="width: 500"]
[tr]
	[td]Sub CountOnesAndZeros()
  Dim R As Long, X As Long, Data As Variant, Result As Variant, OnesAndZeroes() As String
  Data = Range("C5", Cells(Rows.Count, "P").End(xlUp))
  ReDim Result(1 To UBound(Data), 1 To 1)
  For R = 1 To UBound(Data)
    OnesAndZeroes = Split(Replace(Replace(Join(Application.Index(Data, R, 0), ""), "01", "0|1"), "10", "1|0"), "|")
    For X = 0 To UBound(OnesAndZeroes)
      OnesAndZeroes(X) = Len(OnesAndZeroes(X))
    Next
    Result(R, 1) = Data(R, 1) & " - " & Join(OnesAndZeroes, " | ")
  Next
  Range("R5").Resize(UBound(Result)) = Result
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Here is another (more compact) macro that should also work...
Code:
[TABLE="width: 500"]
<TBODY>[TR]
[TD]Sub CountOnesAndZeros()
  Dim R As Long, X As Long, Data As Variant, Result As Variant, OnesAndZeroes() As String
  Data = Range("C5", Cells(Rows.Count, "P").End(xlUp))
  ReDim Result(1 To UBound(Data), 1 To 1)
  For R = 1 To UBound(Data)
    OnesAndZeroes = Split(Replace(Replace(Join(Application.Index(Data, R, 0), ""), "01", "0|1"), "10", "1|0"), "|")
    For X = 0 To UBound(OnesAndZeroes)
      OnesAndZeroes(X) = Len(OnesAndZeroes(X))
    Next
    Result(R, 1) = Data(R, 1) & " - " & Join(OnesAndZeroes, " | ")
  Next
  Range("R5").Resize(UBound(Result)) = Result
End Sub
[/TD]
[/TR]
</TBODY>[/TABLE]
Thank you so much Rick, I do appreciate your help, compact code worked fine!! </SPAN></SPAN>

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

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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