+ Reply to Thread
Results 1 to 7 of 7

Split Delimited Data in Columns into New Rows and keep all other data

Hybrid View

  1. #1
    Registered User
    Join Date
    10-07-2020
    Location
    Singapore
    MS-Off Ver
    2016
    Posts
    4

    Split Delimited Data in Columns into New Rows and keep all other data

    HI,

    I'm new to VBA and I am trying to split the data in my columns into separate rows while keeping the 1st column as well as the header intact. I have an existing code which allows me to split column B but I'm trying to edit the code such that I am able to split the data starting from Column B all the way to the last column. Can anyone kindly advice on how to do that? I've attached the excel file with the data as well as the below macro already in there.

    Thank you and I sincerely appreciate the help!

    ____________________________________________________________________________________________________________________________

    Option Explicit
    
    Const ANALYSIS_ROW As String = "B"
    Const DATA_START_ROW As Long = 1
    
    Sub ReplicateData()
        Dim iRow As Long
        Dim lastrow As Long
        Dim ws As Worksheet
        Dim iSplit() As String
        Dim iIndex As Long
        Dim iSize As Long
    
        'Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
    
        With ThisWorkbook
            .Worksheets("Concertina").Copy After:=.Worksheets("Concertina")
            Set ws = ActiveSheet
        End With
    
        With ws
            lastrow = .Cells(.Rows.Count, ANALYSIS_ROW).End(xlUp).Row
        End With
    
    
        For iRow = lastrow To DATA_START_ROW Step -1
            iSplit = Split(ws.Cells(iRow, ANALYSIS_ROW).Value2, ",")
            iSize = UBound(iSplit) - LBound(iSplit) + 1
            If iSize = 1 Then GoTo Continue
    
            ws.Rows(iRow).Copy
            ws.Rows(iRow).Resize(iSize - 1).Insert
            For iIndex = LBound(iSplit) To UBound(iSplit)
                ws.Cells(iRow, ANALYSIS_ROW).Offset(iIndex).Value2 = iSplit(iIndex)
            Next iIndex
    Continue:
        Next iRow
        Application.CutCopyMode = False
        Application.Calculation = xlCalculationAutomatic
        'Application.ScreenUpdating = True
        
        
    End Sub
    Attached Files Attached Files
    Last edited by alansidman; 10-08-2020 at 05:22 AM.

  2. #2
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    Re: Split Delimited Data in Columns into New Rows and keep all other data

    You will be asked to add code tags - see rules - to make for easier reading

    is this what you want
    Sub Testsplit()
    Dim lcol As Long, i, ii, lrow, nextrow
    Dim arr: Dim ws As Worksheet: Dim ws2 As Worksheet
    Dim check As Boolean
    For Each ws In Worksheets
    If ws.Name Like "Temp" Then check = True: Exit For
    Next
    If check = True Then
    Else
    Worksheets.Add.Name = "Temp"
    End If
    
    Set ws = Sheets("Concertina"): Set ws2 = Sheets("Temp")
    nextrow = ws2.Cells(Rows.Count, 1).End(xlUp).Row
    With ws
    lcol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    For i = 2 To lcol
        lrow = .Cells(Rows.Count, 1).End(xlUp).Row
            For ii = 2 To lrow
                arr = Split(.Cells(ii, i), ",")
                    ws2.Cells(nextrow, 1).Offset(1, 0).Resize(, UBound(arr) + 1).Value = (arr)
                nextrow = nextrow + 1
            Next ii
                    
    Next i
    End With
    End Sub

  3. #3
    Registered User
    Join Date
    10-07-2020
    Location
    Singapore
    MS-Off Ver
    2016
    Posts
    4

    Re: Split Delimited Data in Columns into New Rows and keep all other data

    I'm so sorry, this is the first time that I am posting a coding question so I'm not familiar with code tags...

    I would like to split the data in columns C, D, E, F, all the way to the last column that has data into rows but keeping the header as well as the information in column A. I've attached an example of how Column B should look like after splitting. Column C, D, E and so forth should follow suit and split into rows as well. The number of columns should still remain the same, just the number of rows will increase. I hope my explanation is clear...

    Attachment 698587
    Last edited by VBA_123; 10-07-2020 at 07:24 AM.

  4. #4
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    Re: Split Delimited Data in Columns into New Rows and keep all other data

    Hi, Attachment invalid so cant see. I'm not sure I understand your request either

  5. #5
    Registered User
    Join Date
    10-07-2020
    Location
    Singapore
    MS-Off Ver
    2016
    Posts
    4

    Re: Split Delimited Data in Columns into New Rows and keep all other data

    May I know if you are able to open the attached excel file testdatabase.xlsm?
    The macro "replicate data" does exactly what I want, just that it now only works on column B.
    I would like it to do the same for Column C, D, E (splitting by delimiter) until the last column (which in this case is column P) that has data in it.
    Attached Files Attached Files
    Last edited by VBA_123; 10-07-2020 at 07:40 AM.

  6. #6
    Registered User
    Join Date
    10-07-2020
    Location
    Singapore
    MS-Off Ver
    2016
    Posts
    4

    Re: Split Delimited Data in Columns into New Rows and keep all other data

    Post deleted
    Last edited by VBA_123; 10-08-2020 at 12:14 AM. Reason: Delete post as I realise my logic for what I was trying to achieve is wrong

  7. #7
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,694

    Re: Split Delimited Data in Columns into New Rows and keep all other data

    Code Tags Added
    Your post does not comply with Rule 2 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. split delimited data into rows - how???
    By elizevonne in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-31-2020, 06:25 PM
  2. [SOLVED] Split data from columns to rows
    By tax112 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-04-2019, 05:44 AM
  3. [SOLVED] Split Delimited nth Data into New Rows/columns
    By adeel1 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-29-2018, 01:55 AM
  4. Vba code to split data with delimited "," in separate rows
    By gaikwad.mm@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 04-26-2017, 02:26 AM
  5. Delimited Data - Columns and Rows
    By Arch88 in forum Excel General
    Replies: 1
    Last Post: 06-01-2016, 07:11 AM
  6. [SOLVED] Multi columns of delimited data into rows
    By DWTK in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-18-2013, 11:18 AM
  7. Split one row of data to columns and rows
    By A7md in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 10-12-2011, 05:52 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1