Count consecutive occurrences of only 1

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,371
Office Version
  1. 2010
Happy New Year to everyone

Hello,

I am looking VBA solution, which count the constant occurrences of 1 only

Data got in cells C6:P22,

Results shown in R6 AF22

Sample Data


Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAH
1
2
3
4
5P1P2P3P4P5P6P7P8P9P10P11P12P13P14P1P2P3P4P5P6P7P8P9P10P11P12P13P14P15
6o1ooo1111o11oo142
7oo1o1oooo1ooo11111
8oo1o1oo11oooo11121
9oo11ooo1o1oooo211
101oo1o1111o111o1143
111ooo1o11oo11111124
12oo1ooooo1111oo14
13o11o1o11111o112152
141ooooo1o1o11111114
15ooooo111o1o11o312
16111o1111oo1o113412
17oo1oooo1o111oo113
18oo1o1oo11o11o111221
19oooooo1o1111oo14
201o11o1111oooo11241
21ooooooooooooo11
22111111o111111167
23
Sheet1



Thank you all
Excel 2000
Regards,
Moti
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
See if this works for you:
Code:
Sub Test()
    Dim tmparr, i As Long, j As Long
    For i = 6 To 22
        tmparr = Split(WorksheetFunction.Trim(Replace(Join( _
            WorksheetFunction.Transpose(WorksheetFunction.Transpose( _
            Range("C" & i & ":P" & i))), ""), "o", " ")), " ")
        For j = 0 To UBound(tmparr)
            Cells(i, "R").Offset(0, j) = Len(tmparr(j))
        Next j
    Next i
End Sub
 
Upvote 0
See if this works for you:
Code:
Sub Test()
    Dim tmparr, i As Long, j As Long
    For i = 6 To 22
        tmparr = Split(WorksheetFunction.Trim(Replace(Join( _
            WorksheetFunction.Transpose(WorksheetFunction.Transpose( _
            Range("C" & i & ":P" & i))), ""), "o", " ")), " ")
        For j = 0 To UBound(tmparr)
            Cells(i, "R").Offset(0, j) = Len(tmparr(j))
        Next j
    Next i
End Sub
Tetra201, absolutely perfect!

Thank you so much for your kind help

Regards,
Moti

 
Upvote 0
See if this works for you:
Code:
Sub Test()
    Dim tmparr, i As Long, j As Long
    For i = 6 To 22
        [B][COLOR="#FF0000"]tmparr = Split(WorksheetFunction.Trim(Replace(Join( _
            WorksheetFunction.Transpose(WorksheetFunction.Transpose( _
            Range("C" & i & ":P" & i))), ""), "o", " ")), " ")[/COLOR][/B]
        For j = 0 To UBound(tmparr)
            Cells(i, "R").Offset(0, j) = Len(tmparr(j))
        Next j
    Next i
End Sub
Instead of using two WorksheetFunction.Transpose function calls, you can use a single WorksheetFunction.Index call instead...
Code:
tmparr = Split(WorksheetFunction.Trim(Replace(Join(WorksheetFunction.Index( _
         Range("C" & i & ":P" & i).Value, 1, 0), ""), "o", " ")), " ")
 
Upvote 0
Instead of using two WorksheetFunction.Transpose function calls, you can use a single WorksheetFunction.Index call instead...
Code:
tmparr = Split(WorksheetFunction.Trim(Replace(Join(WorksheetFunction.Index( _
         Range("C" & i & ":P" & i).Value, 1, 0), ""), "o", " ")), " ")
Hello Rick, I don't know what is the difference but anyhow I have replaced it as you suggested

Thank you for the help and suggestion.

Regards,
Moti


 
Last edited:
Upvote 0
Hello Rick, I don't know what is the difference but anyhow I have replaced it as you suggested
Probably not noticeable unless you have a humongous amount of data to process, but one Index function call is more efficient than two Transpose function calls.
 
Upvote 0
Probably not noticeable unless you have a humongous amount of data to process, but one Index function call is more efficient than two Transpose function calls.
Hello Rick, thank you very much for the explanation. Now I understand it can cause error with large amount of data. Really I am using large numbers so I already changed it for safety.

Regards,
Moti
 
Upvote 0
Rick,
Thank you for the clever tip (y)
You are quite welcome. One thing I probably should mention... explicitly specifying the Value property on the Range reference is required to make the function call work.
 
Upvote 0

Forum statistics

Threads
1,215,777
Messages
6,126,836
Members
449,343
Latest member
DEWS2031

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