Search code examples
excelexcel-formulaexcel-2010excel-2007vba

How to Split a single cell into multiple rows and add another row


I have a table that has two Columns. Date and Test Name. What I would like to happen is that the string of text in one single cell be separated into multiple rows. In addition, I need the date to be associated with each set of text. I have tried text to columns and then transpose, but it can only handle 1 set of string at a time and not the entire data set.

enter image description here


Solution

  • Loop through Column A then loop through the string next to it.

    Results will be in column D

        Sub ChickatAH()
        Dim rng As Range, Lstrw As Long, c As Range
        Dim SpltRng As Range
        Dim i As Integer
        Dim Orig As Variant
        Dim txt As String
    
        Lstrw = Cells(Rows.Count, "A").End(xlUp).Row
        Set rng = Range("A2:A" & Lstrw)
    
        For Each c In rng.Cells
            Set SpltRng = c.Offset(, 1)
            txt = SpltRng.Value
            Orig = Split(txt, " ")
    
            For i = 0 To UBound(Orig)
                Cells(Rows.Count, "D").End(xlUp).Offset(1) = c
                Cells(Rows.Count, "D").End(xlUp).Offset(, 1) = Orig(i)
            Next i
    
        Next c
    
    End Sub