SOLVED

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

%3CLINGO-SUB%20id%3D%22lingo-sub-1716184%22%20slang%3D%22de-DE%22%3ECount%20in%20a%20row%20until%20content%20changes%2C%20then%20count%20this%20next%20content%20until%20it%20changes%20and%20so%20on%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1716184%22%20slang%3D%22de-DE%22%3E%3CP%3EHello%20Community%2C%3C%2FP%3E%3CP%3Eit%20is%20easier%20to%20see%20what%20I%20am%20looking%20for%2C%20than%20to%20explain.%20I%20have%20a%20table%20with%2015%20columns%20and%20a%20lot%20of%20rows.%20In%20each%20column%20is%20a%20letter%20and%20in%20a%20row%2C%20they%20change%20a%20lot.%20They%20are%20not%20(and%20should%20not%20be)%20in%20an%20alphabetical%20order.%20It%20is%20a%20created%20pattern%20for%20working%20with%20pearls.%20I%20would%20like%20to%20summarize%20each%20row%20as%20follows%3A%3C%2FP%3E%3CP%3EExample%20row%3A%20D%20B%20B%20B%20R%20G%20B%20B%20B%20B%20O%20O%20O%20B%20B%3C%2FP%3E%3CP%3EDesired%20result%3A%201xD%2C%203xB%2C%201xR%2C%201xG%2C%204xB%2C%203xO%2C%202xB%3C%2FP%3E%3CP%3EI%20do%20NOT%20search%20the%20result%20like%201xD%2C%209xB%2C%20etc...%20I%20need%20the%20count%20of%20the%20letters%20in%20the%20correct%20order.%3C%2FP%3E%3CP%3EIn%20the%20end%20I%20will%20need%20the%20VBA%20Code%2C%20but%20if%20I%20would%20know%20the%20correct%20functions%20which%20I%20have%20to%20combine%2C%20this%20would%20be%20an%20appreciated%20start.%3C%2FP%3E%3CP%3EThanks%20very%20much%20and%20best%20regards%2C%3C%2FP%3E%3CP%3EClaudia%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1716184%22%20slang%3D%22de-DE%22%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1716260%22%20slang%3D%22en-US%22%3ERe%3A%20Count%20in%20a%20row%20until%20content%20changes%2C%20then%20count%20this%20next%20content%20until%20it%20changes%20and%20so%20on%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1716260%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F810154%22%20target%3D%22_blank%22%3E%40Jungdra%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECreate%20the%20following%20custom%20VBA%20function%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3EFunction%20CountLetters(rng%20As%20Range)%20As%20String%0A%20%20%20%20Dim%20i%20As%20Long%0A%20%20%20%20Dim%20s%20As%20String%0A%20%20%20%20Dim%20c%20As%20String%0A%20%20%20%20Dim%20n%20As%20Long%0A%20%20%20%20%0A%20%20%20%20c%20%3D%20rng(1).Value%0A%20%20%20%20For%20i%20%3D%201%20To%20rng.Count%0A%20%20%20%20%20%20%20%20If%20rng(i).Value%20%3D%20c%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20n%20%3D%20n%20%2B%201%0A%20%20%20%20%20%20%20%20Else%0A%20%20%20%20%20%20%20%20%20%20%20%20If%20i%20%26gt%3B%201%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20s%20%3D%20s%20%26amp%3B%20%22%2C%20%22%20%26amp%3B%20n%20%26amp%3B%20%22x%22%20%26amp%3B%20c%0A%20%20%20%20%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20%20%20%20%20c%20%3D%20rng(i).Value%0A%20%20%20%20%20%20%20%20%20%20%20%20n%20%3D%201%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20Next%20i%0A%20%20%20%20s%20%3D%20s%20%26amp%3B%20%22%2C%20%22%20%26amp%3B%20n%20%26amp%3B%20%22x%22%20%26amp%3B%20c%0A%20%20%20%20CountLetters%20%3D%20Mid(s%2C%203)%0AEnd%20Function%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3ELet's%20say%20the%20first%20row%20with%20letters%20is%20A2%3AO2.%3C%2FP%3E%0A%3CP%3EEnter%20the%20following%20formula%20in%20P2%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3DCountLetters(A2%3AO2)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFill%20down%20to%20the%20last%20row%20with%20letters.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1733418%22%20slang%3D%22de-DE%22%3ERe%3A%20Count%20in%20a%20row%20until%20content%20changes%2C%20then%20count%20this%20next%20content%20until%20it%20changes%20and%20so%20on%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1733418%22%20slang%3D%22de-DE%22%3EHello%20Hans%2C%20%3CBR%20%2F%3E%20sorry%2C%20I%20had%20expected%20a%20notification%20at%20reply%2C%20but%20there%20was%20none.%20Have%20just%20discovered%20your%20solution.%20I'm%20looking%20forward%20to%20trying%20them%20out%2C%20but%20that%20has%20to%20wait%20a%20few%20days.%20Thank%20you%20very%20much%20in%20advance!%3CBR%20%2F%3EGreetings%2C%20%3CBR%20%2F%3E%20Claudia%3C%2FLINGO-BODY%3E
New Contributor

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 not (and should not be) in an alphabetical order. It is a created pattern for working with pearls. I would like to summarize each row as follows:

Example row: D B B B R G B B B B O O O B B

Desired result: 1xD, 3xB, 1xR, 1xG, 4xB, 3xO, 2xB

I do NOT search the result like 1xD, 9xB, etc... I need the count of the letters in the correct order.

In the end I will need the VBA Code, but if I would know the correct functions which I have to combine, this would be an appreciated start.

Thanks very much and best regards,

Claudia

2 Replies
Best Response confirmed by Jungdra (New Contributor)
Solution

@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.

 

Hallo Hans,
entschuldigung, ich hatte eine Benachrichtigung bei Antwort erwartet, aber es kam keine. Habe Deine Lösung gerade erst entdeckt. Freue mich drauf sie auszuprobieren, aber das muss ein paar Tage warten. Vielen lieben Dank schon mal vorab!
Viele Grüße,
Claudia