Question : Need Algorithm: Count The Number of Rows Multiple Times

Hey guys,

I have a general programming concept that I cannot tackle very well..

So I have a column of numbers (only 0s and 1s) and need to display the start and stop position for all of the 1s. For example

1
1
1
0
0
1
1
0
1
1
1
1
0
1
0

would output:
1-3, 6-7, 9-12, 14

I've created different loops and had a few ideas, but cannot grasp this idea completely right now.  It may be because it's Friday or that I'm retarded. One of the two.  If anyone could help, it would be greatly appreciated.

Thanks!
Rob

Answer : Need Algorithm: Count The Number of Rows Multiple Times

I haven't tested the other suggestions, so my apologies if I am offering something that's already been tried :)

This works as a function.  So, if you have your 1s and 0s in, say, A1:A50, then in another cell you would use:

=MakeTheList(A1:A50)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
Function MakeTheList(ArrayIn As Variant)
    
    Dim x As Variant
    Dim StartAt As Long, EndAt As Long
    Dim Counter As Long
    
    Const Delimiter As String = ", "
    
    For Each x In ArrayIn
        Counter = Counter + 1
        If x <> 0 Then
            EndAt = Counter
            If StartAt = 0 Then StartAt = Counter
        Else
            If EndAt <> 0 Then
                If StartAt <> EndAt Then
                    MakeTheList = MakeTheList & Delimiter & StartAt & "-" & EndAt
                Else
                    MakeTheList = MakeTheList & Delimiter & StartAt
                End If
                StartAt = 0
                EndAt = 0
            End If
        End If
    Next
    
    If StartAt <> 0 Then
        If StartAt <> EndAt Then
            MakeTheList = MakeTheList & Delimiter & StartAt & "-" & EndAt
        Else
            MakeTheList = MakeTheList & Delimiter & StartAt
        End If
    End If
    
    MakeTheList = Mid(MakeTheList, Len(Delimiter))
    
End Function
Random Solutions  
 
programming4us programming4us