Count consecutive occurrences of only 1

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,422
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

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
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

Forum statistics

Threads
1,224,749
Messages
6,180,725
Members
452,995
Latest member
isldboy

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