Forum Discussion

diegohoutart's avatar
diegohoutart
Copper Contributor
Nov 27, 2019

Convert list of data to table

Hello everyone,

I have a data set of this form:

 

AuthorAuthor1
TitleTitle1
DateDate1
AuthorAuthor2
TitleTitle2
DateDate2
AuthorAuthor3
TitleTitle3
DateDate3

 

And i would like to convert it to a table of that kind:

AuthorTitleDate
Author1Title1Date1
Author2Title2Date2
Author3Title3Date3

 

Off course there's more than 3 books to sort ๐Ÿ˜‰

Title1, Author2,... are just sample values for Title, Author,...

Any idea that could help me ?

 

Thank you so much for your help !

 

Diego

4 Replies

  • diegohoutart This should do what you want. It will build a new list in the format you want, starting in cell D1, so make sure D:F are empty.

     

    Sub SplitAuthors()
        Dim i As Long
        Dim lr1 As Long
        Dim lr2 As Long
        
        lr1 = Cells(Rows.Count, "B").End(xlUp).Row
        
        Range("D1").Value = "Author"
        Range("E1").Value = "Title"
        Range("F1").Value = "Date"
        
        For i = 1 To lr1 Step 3
            lr2 = Cells(Rows.Count, "D").End(xlUp).Offset(1).Row
            Cells(lr2, "D").Value = Cells(i, "B").Value
            Cells(lr2, "E").Value = Cells(i + 1, "B").Value
            Cells(lr2, "F").Value = Cells(i + 2, "B").Value
        Next i
            
    End Sub

     

      • Smitty Smith's avatar
        Smitty Smith
        Former Employee

        diegohoutart You can right-click any sheet tab, select View Code, which will open the Visual Basic Editor (VBE). Then go to Insert > Module, and paste the code in the new window that opens on the right. Alt+Q will exit you back to Excel. You can run the code by selecting it from the Macros pane (Alt+T+M+M), then press Run. You can also run it from the https://support.office.com/en-us/article/Show-the-Developer-tab-E1192344-5E56-4D45-931B-E5FD9BEA2D45 > Macros.

         

        Sergei - That's a great solution. I had thought about doing it with PQ, but didn't even think of using an Index & divisor.

Resources