+ Reply to Thread
Results 1 to 5 of 5

Want to split a table based on values in a column

Hybrid View

  1. #1
    Registered User
    Join Date
    04-20-2020
    Location
    Kerala, India.
    MS-Off Ver
    Office 2016
    Posts
    11

    Want to split a table based on values in a column

    Hi all,

    I want to split data in a table based on a column, where the column is having multiple values separated by a "/". All those separate values should form a separate line in the table.
    Also, data in said column has non-uniform spacing which you can see if you give a closer look.

    Thanks in advance.

    Dominic.
    Attached Files Attached Files

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Want to split a table based on values in a column

    Assuming the data actually starts in row 1 try this.
    Option Explicit
    
    Sub ExpandTable()
    Dim rngSrc As Range
    Dim arrIn As Variant
    Dim arrOut As Variant
    Dim arrVals As Variant
    Dim cnt As Long
    Dim idxRow As Long
    Dim idxVals As Long
    Dim NoRows As Long
    
        Set rngSrc = Range("A2:D" & Range("A" & Rows.Count).End(xlUp).Row)
    
        NoRows = Evaluate("=SUM(LEN(" & rngSrc.Columns(2).Address & ")-LEN(SUBSTITUTE(" & rngSrc.Columns(2).Address & ", ""/"", """")))") + rngSrc.Rows.Count
        
        arrIn = rngSrc.Value
        
        ReDim arrOut(1 To NoRows, 1 To 4)
        
        For idxRow = 1 To UBound(arrIn, 2)
        
            arrVals = Split(arrIn(idxRow, 2), "/")
            
            For idxVals = LBound(arrVals) To UBound(arrVals)
                cnt = cnt + 1
                arrOut(cnt, 1) = arrIn(idxRow, 1)
                arrOut(cnt, 2) = Trim(arrVals(idxVals))
                arrOut(cnt, 3) = arrIn(idxRow, 3)
                arrOut(cnt, 4) = arrIn(idxRow, 4)
            Next idxVals
            
        Next idxRow
        
        With rngSrc.Offset(, 7)
            With .Offset(-1).Resize(1, 4)
                .Borders.Weight = xlThin
                .Font.Bold = True
                .HorizontalAlignment = xlHAlignCenter
                .Value = Array("Item Name", "Quantity", "Unit", "Date of order")
            End With
            With .Resize(NoRows)
                .Borders.Weight = xlThin
                .HorizontalAlignment = xlHAlignCenter
                .Value = arrOut
            End With
            .EntireColumn.AutoFit
        End With
        
    End Sub
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    04-20-2020
    Location
    Kerala, India.
    MS-Off Ver
    Office 2016
    Posts
    11

    Re: Want to split a table based on values in a column

    It worked for me. Thank you so much. But after i put the code in my excel sheet, i am getting the message, "Be careful ! Parts of your document may include personal information that can't be removed by the document inspector", even when i am trying to save other excel workbooks other that the one in which i pasted the code. Do you have any idea why the message is coming up ?

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2406 Win 11 Home 64 Bit
    Posts
    23,961

    Re: Want to split a table based on values in a column

    Here is an alternative solution. Using Power Query. Here is the Mcode. File attached for analysis.
    let
        Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item name", type text}, {"Quantity", type any}, {"Unit", type text}, {"Date of order", type date}}),
        #"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(Table.TransformColumnTypes(#"Changed Type", {{"Quantity", type text}}, "en-US"), {{"Quantity", Splitter.SplitTextByDelimiter("/", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Quantity"),
        #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Quantity", Int64.Type}})
    in
        #"Changed Type1"
    Attached Files Attached Files
    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

  5. #5
    Registered User
    Join Date
    04-20-2020
    Location
    Kerala, India.
    MS-Off Ver
    Office 2016
    Posts
    11

    Re: Want to split a table based on values in a column

    It worked for me Alan. Thank you.

+ 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. Need Macro code to split data split in 7 sheets based on variable rows in column A
    By Alija_21 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-31-2015, 08:03 AM
  2. Split one Excel file into multiple files based on values in column A?
    By MetroBOS in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-06-2015, 10:59 AM
  3. [SOLVED] Split data in table into 3 tables based on column value
    By adelkam in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-08-2015, 05:09 AM
  4. Replies: 0
    Last Post: 09-03-2014, 04:38 PM
  5. How Do I Split Text Based Upon Two Values To Split
    By HowdeeDoodee in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-08-2012, 05:05 AM
  6. [SOLVED] How To Split Excel File Into Separate Workbooks Based on Values In a Specific Column
    By UzieJacuzzi in forum Excel Programming / VBA / Macros
    Replies: 30
    Last Post: 07-31-2012, 07:26 AM
  7. Function to split up a table based upon the values in a reference column
    By alexdragne in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-08-2012, 02:45 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