+ Reply to Thread
Results 1 to 7 of 7

Need A Macro that takes comma delimited values in select columns and seperates them to row

Hybrid View

  1. #1
    Registered User
    Join Date
    01-15-2015
    Location
    Pennsylvania
    MS-Off Ver
    MS Excel 2013
    Posts
    3

    Need A Macro that takes comma delimited values in select columns and seperates them to row

    Hello Everyone,

    New to the forum and looking for some help,

    I have a large ecomm spreadsheet that comes from vendors with multiple item attributes in a single cell in a number of columns. I need the macro to go through select columns and seperate these comma delimited attributes to a newly inserted row below the original while keeping the first attribute in the original cell.

    Attached is my current code in VB(be gentle, I was never taught VB), it works to an extent, however, it does not move past the first row. And if, say column B has 3 attributes in row 1, and 5 attributes in column D of row 1, the last 2 of column D are being chopped off. I need it to not cut these off and be OK with later columns have more inserted rows.

    TroyMacro.txt

    Any help on this would be great,

    Thanks to everyone in advance.

    I am not able to attach a legitimate smaple workbook, but I can attach a screenshot of what im seeingvs what I need. I hope that this will help.MACROexample.png

    Thanks,
    Last edited by JBeaucaire; 01-15-2015 at 07:52 PM.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,922

    Re: Need A Macro that takes comma delimited values in select columns and seperates them to

    Please post your code directly to the forum in the post, using code tags. (It's easier than attaching a text file with the code that others have to download and open to read):
    [CODE]Your code lines between tags[/CODE] results --->
    your code lines
    Ben Van Johnson

  3. #3
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,922

    Re: Need A Macro that takes comma delimited values in select columns and seperates them to

    Option Explicit
    Sub Split_BD()
    
        Dim ArrPtr      As Long, _
            RecCount    As Long, _
            MaxBD       As Long, _
            DestRow     As Long, _
            Split_B()   As String, _
            Split_D()   As String, _
            TestCell    As Variant, _
            DEST        As Worksheet, _
            SOURCE      As Worksheet
    
        Set SOURCE = Sheets("Sheet1")
        Application.DisplayAlerts = False
        On Error Resume Next
        Sheets("Workspace").Delete
        On Error GoTo 0
        
        Sheets.Add after:=Sheets(Sheets.Count)
        ActiveSheet.Name = "WorkSpace"
        Set DEST = ActiveSheet
        Application.DisplayAlerts = True
        
        With Worksheets("Sheet1")
            RecCount = .Cells(.Rows.Count, "A").End(xlUp).Row
    
            For Each TestCell In .Range("A1:A" & RecCount)
                Split_B = Split(TestCell.Offset(columnoffset:=1).Value, ",")
                Split_D = Split(TestCell.Offset(columnoffset:=3).Value, ",")
                
                'get the larger upper bound of the two arrays
                MaxBD = WorksheetFunction.Max(UBound(Split_B), UBound(Split_D))
                
                'make both arrays same size
                'the previously smaller one will have a blank last element
                
                ReDim Preserve Split_B(0 To MaxBD)
                ReDim Preserve Split_D(0 To MaxBD)
                
                For ArrPtr = 0 To MaxBD
                    DestRow = DestRow + 1
                    
                    'write column A & C once for each group
                    If ArrPtr = 0 Then
                        DEST.Cells(DestRow, "A").Value = TestCell.Value
                        DEST.Cells(DestRow, "C").Value = TestCell.Offset(columnoffset:=2).Value
                    End If
                    
                    'write all array elements
                    DEST.Cells(DestRow, "B").Value = Split_B(ArrPtr)
                    DEST.Cells(DestRow, "D").Value = Split_D(ArrPtr)
                Next ArrPtr
            Next TestCell
        End With
    End Sub

  4. #4
    Registered User
    Join Date
    01-15-2015
    Location
    Pennsylvania
    MS-Off Ver
    MS Excel 2013
    Posts
    3

    Re: Need A Macro that takes comma delimited values in select columns and seperates them to

    Thank you protonLeah,

    One question, how would I extent this code to incorporate other columns? could you please include an example of the code as it would be for an additional column to be added.

    Thank you,

  5. #5
    Forum Contributor
    Join Date
    12-29-2014
    Location
    Indonesia
    MS-Off Ver
    Office 2010, 2013
    Posts
    125

    Re: Need A Macro that takes comma delimited values in select columns and seperates them to

    Hi all,
    please allow me to join this thread...thank you.
    Regard

  6. #6
    Registered User
    Join Date
    01-15-2015
    Location
    Pennsylvania
    MS-Off Ver
    MS Excel 2013
    Posts
    3

    Re: Need A Macro that takes comma delimited values in select columns and seperates them to

    protonLeah,

    Thank you very much for this, worked very well for my purposes.

    Thanks,

    Troy Behmer

  7. #7
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,922

    Re: Need A Macro that takes comma delimited values in select columns and seperates them to

    Assume columns C & E shall not be split; B, D & F are split

    Option Explicit
    Sub Split_BDF()
    
        Dim ArrPtr      As Long, _
            RecCount    As Long, _
            MaxBD       As Long, _
            DestRow     As Long, _
            Split_B()   As String, _
            Split_D()   As String, _
            Split_F()   As String, _
            TestCell    As Variant, _
            DEST        As Worksheet, _
            SOURCE      As Worksheet
    
        Set SOURCE = Sheets("Sheet1")
        Application.DisplayAlerts = False
        On Error Resume Next
        Sheets("Workspace").Delete
        On Error GoTo 0
        
        Sheets.Add after:=Sheets(Sheets.Count)
        ActiveSheet.Name = "WorkSpace"
        Set DEST = ActiveSheet
        Application.DisplayAlerts = True
        
        With SOURCE
            RecCount = .Cells(.Rows.Count, "A").End(xlUp).Row
    
            For Each TestCell In .Range("A1:A" & RecCount)
                'columns B, D & F shall be split
                Split_B = Split(TestCell.Offset(columnoffset:=1).Value, ",")
                Split_D = Split(TestCell.Offset(columnoffset:=3).Value, ",")
                Split_F = Split(TestCell.Offset(columnoffset:=5).Value, ",")
                
                'get the larger upper bound of the two arrays
                MaxBD = WorksheetFunction.Max(UBound(Split_B), UBound(Split_D), UBound(Split_F))
                
                'make both arrays same size
                'the previously smaller one will have a blank last element
                
                ReDim Preserve Split_B(0 To MaxBD)
                ReDim Preserve Split_D(0 To MaxBD)
                ReDim Preserve Split_F(0 To MaxBD)
                
                For ArrPtr = 0 To MaxBD
                    DestRow = DestRow + 1
                    
                    'write column A, C & E once only for each group
                    If ArrPtr = 0 Then
                        DEST.Cells(DestRow, "A").Value = TestCell.Value
                        DEST.Cells(DestRow, "C").Value = TestCell.Offset(columnoffset:=2).Value
                        DEST.Cells(DestRow, "E").Value = TestCell.Offset(columnoffset:=4).Value
                    End If
                    
                    'write all array elements
                    DEST.Cells(DestRow, "B").Value = Split_B(ArrPtr)
                    DEST.Cells(DestRow, "D").Value = Split_D(ArrPtr)
                    DEST.Cells(DestRow, "F").Value = Split_F(ArrPtr)
                Next ArrPtr
            Next TestCell
        End With     'SOURCE
    End Sub
    Last edited by protonLeah; 01-21-2015 at 12:50 AM.

+ 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. Separate comma-delimited values to rows
    By sporto in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 02-22-2013, 10:06 AM
  2. Splitting comma delimited numbers into new columns
    By salmansohail in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-30-2010, 07:00 PM
  3. SUM of values in a comma delimited cell
    By Danexcel in forum Excel General
    Replies: 18
    Last Post: 01-15-2010, 10:59 AM
  4. Splitting comma delimited numbers into new columns
    By Cicada in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 04-23-2009, 07:30 PM
  5. separating values in comma delimited cells
    By JChandler22 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-20-2008, 06:31 PM

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