Forum Discussion

Jungdra's avatar
Jungdra
Copper Contributor
Sep 26, 2020
Solved

Count in a row until content changes, then count this next content until it changes and so on

Hello Community, it is easier to see what I am looking for, than to explain. I have a table with 15 columns and a lot of rows. In each column is a letter and in a row, they change a lot. They are no...
  • HansVogelaar's avatar
    Sep 26, 2020

    Jungdra 

    Create the following custom VBA function:

    Function CountLetters(rng As Range) As String
        Dim i As Long
        Dim s As String
        Dim c As String
        Dim n As Long
        
        c = rng(1).Value
        For i = 1 To rng.Count
            If rng(i).Value = c Then
                n = n + 1
            Else
                If i > 1 Then
                    s = s & ", " & n & "x" & c
                End If
                c = rng(i).Value
                n = 1
            End If
        Next i
        s = s & ", " & n & "x" & c
        CountLetters = Mid(s, 3)
    End Function

    Let's say the first row with letters is A2:O2.

    Enter the following formula in P2:

     

    =CountLetters(A2:O2)

     

    Fill down to the last row with letters.

     

Resources