+ Reply to Thread
Results 1 to 12 of 12

Comma delimited list cell to new row code help

Hybrid View

  1. #1
    Registered User
    Join Date
    07-07-2011
    Location
    tx
    MS-Off Ver
    Excel 2007
    Posts
    5

    Comma delimited list cell to new row code help

    Hello,

    I am trying to split any cells with multiple entries, separated by a comma, to new rows. Then I need to copy the information from the original row to the newly created rows. I have a code that will work on a test workbook, but when I run it on the workbook I the code for, it will not separate the data. Here is the code and I will attach the two file also.

    Option Explicit
    Public Col As String
    Public daSting As String, Z As Long, daRow As Long
    Public stringLen, daAnsw, X
    
    
    Sub Expand_Data()
        Call movecolQ
        Sheets.Add After:=ActiveSheet
        ActiveSheet.Name = "Expanded"
        Sheets("DATA").Cells.Copy
        ActiveSheet.Range("A1").PasteSpecial
        Call CountCommas
    
    End Sub
    
    
    Sub CountCommas()
        Sheets("Expanded").Select
        daRow = Application.CountA(ActiveSheet.Range("A:A"))
        For Z = 1 To daRow              'How many rows to work on
            daSting = Cells(Z, 1)       'Get string
            stringLen = Len(daSting)        'Length of String
            For X = 1 To stringLen          'Increment thru
                Select Case Mid(daSting, X, 1)
                Case ","                'If it is a comma
                    daAnsw = daAnsw + 1    'Add 1 to list
                Case Else           'Do nothing
                End Select
            Next
            Cells(Z, 25) = daAnsw            'Write the answer
            daAnsw = 0                      'Reset counter
        Next
        Call InsertRows
    End Sub
    
    Sub InsertRows()
        Dim lRows As Long
        Dim iCell As Range
        Dim rng As Range
        Dim LR As Long
    
        Application.ScreenUpdating = False
        LR = Range("Y" & Rows.Count).End(xlUp).Row
    
        Set rng = Range("Y2:Y" & LR)
    
        For Each iCell In rng
            If Not iCell = 0 Then
                lRows = iCell
                iCell = 0
                iCell.Resize(lRows, 1).EntireRow.Insert
                iCell.EntireRow.Copy
                iCell.Offset(0, 0).EntireRow.Select
                Range(iCell, iCell.Offset(-lRows, 0)).EntireRow.PasteSpecial
                Col = Right(iCell.Offset(-lRows, 0).Address, 2)
    
                Call SplitCells
    
            End If
        Next
        Columns(25).ClearContents
        Call origcolQ
        Call firstpagecolQ
        Application.ScreenUpdating = True
        Call msgbox1
        
        
    End Sub
    
    Sub SplitCells()
        Dim i As Long
        With Application
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
            Sheets("Expanded").Range("A" & Col).Select
            For i = 1 To Selection.Rows.Count
                Dim splitValues As Variant
                splitValues = Split(Selection.Rows(i).Value, ",")
                Selection.Rows(i).Resize(UBound(splitValues) - LBound(splitValues) + 1).Value = Application.Transpose(splitValues)
            Next i
            .Calculation = xlCalculationAutomatic
            .ScreenUpdating = True
        End With
    End Sub
    
    Sub movecolQ()
    ActiveSheet.Name = "DATA"
    Columns("Q").Copy
    Columns("A").Insert
    Columns("R").Delete 'or clearcontents
    End Sub
    
     Sub origcolQ()
    Columns("A").Copy
    Columns("R").Insert
    Columns("A").Delete 'or clearcontents
    End Sub
    
     Sub msgbox1()
        MsgBox "Done"
    End Sub
     
     Sub firstpagecolQ()
    Sheets("DATA").Select
    Columns("A").Copy
    Columns("R").Insert
    Columns("A").Delete 'or clearcontents
    Sheets("Expanded").Select
    End Sub
    Thank you for any tips or suggestions. If you have any questions please ask.
    John
    Attached Files Attached Files
    Last edited by hoovopotamus; 07-10-2011 at 03:23 PM. Reason: Problem fixed

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Comma delimited list cell to new row code help

    Hi John

    In this code
    Sub CountCommas()
        Sheets("Expanded").Select
        daRow = Application.CountA(ActiveSheet.Range("A:A"))
        For Z = 1 To daRow              'How many rows to work on
            daSting = Cells(Z, 1)       'Get string
            stringLen = Len(daSting)        'Length of String
            For X = 1 To stringLen          'Increment thru
                Select Case Mid(daSting, X, 1)
                Case ","                'If it is a comma
                    daAnsw = daAnsw + 1    'Add 1 to list
                Case Else           'Do nothing
                End Select
            Next
            Cells(Z, 25) = daAnsw            'Write the answer
            daAnsw = 0                      'Reset counter
        Next
        Call InsertRows
    End Sub
    In the file that works, daRow evaluates to 15 and all your coma delimited cells are within that range...so it works. In the file that doesn't work, daRow evaluates to 87 and all the coma delimited cells fall outside that range and aren't getting evaluated.

    Welcome to the Forum!
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    07-07-2011
    Location
    tx
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Comma delimited list cell to new row code help

    Quote Originally Posted by jaslake View Post

    In the file that works, daRow evaluates to 15 and all your coma delimited cells are within that range...so it works. In the file that doesn't work, daRow evaluates to 87 and all the coma delimited cells fall outside that range and aren't getting evaluated.
    Thank you for your answer. How would I get daRow to encompass all of the delimited cells? Really, it needs to be able to encompass a variable amount of rows since they change almost daily, how would you go about accomplishing that? I am pretty new to writing code, so I apologize if that is a silly question. Thanks again for your time.

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Comma delimited list cell to new row code help

    I haven't dug through it in detail, but I notice that the DoesWork workbook has delimited strings in column Q, while Doesn'tWork has numbers (or blanks) in column Q
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  5. #5
    Registered User
    Join Date
    07-07-2011
    Location
    tx
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Comma delimited list cell to new row code help

    Quote Originally Posted by mikerickson View Post
    I haven't dug through it in detail, but I notice that the DoesWork workbook has delimited strings in column Q, while Doesn'tWork has numbers (or blanks) in column Q
    The first delimited string is in row #89 in the Doesn't Work column.

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Comma delimited list cell to new row code help

    Hi John
    I've some ideas...I'll post back this afternoon.

  7. #7
    Registered User
    Join Date
    07-07-2011
    Location
    tx
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Comma delimited list cell to new row code help

    Thank you I really appreciate it.

  8. #8
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Comma delimited list cell to new row code help

    Hi John

    This code has been tested on both books and appears do do as you require. This is the code for CountCommas
    Private Sub CountCommas()
        Dim daCell As Range
        Sheets("Expanded").Select
        '    daRow = Application.CountA(ActiveSheet.Range("A:A"))
        For Each daCell In Columns("A:A").SpecialCells(xlConstants)
            '    For Z = 1 To daRow              'How many rows to work on
            daSting = daCell.Text       'Get string
            stringLen = Len(daSting)        'Length of String
            For X = 1 To stringLen          'Increment thru
                Select Case Mid(daSting, X, 1)
                Case ","                'If it is a comma
                    daAnsw = daAnsw + 1    'Add 1 to list
                Case Else           'Do nothing
                End Select
            Next
            Cells(daCell.Row, 25) = daAnsw            'Write the answer
            daAnsw = 0                      'Reset counter
        Next
        Call InsertRows
    End Sub
    And this is the code for InsertRows (you'll see that I changed one line of code)
    Sub InsertRows()
        Dim lRows As Long
        Dim iCell As Range
        Dim rng As Range
        Dim LR As Long
        Application.ScreenUpdating = False
        LR = Range("Y" & Rows.Count).End(xlUp).Row
        Set rng = Range("Y2:Y" & LR)
        For Each iCell In rng
            If Not iCell = 0 Then
                lRows = iCell
                iCell = 0
                iCell.Resize(lRows, 1).EntireRow.Insert
                iCell.EntireRow.Copy
                iCell.Offset(0, 0).EntireRow.Select
                Range(iCell, iCell.Offset(-lRows, 0)).EntireRow.PasteSpecial
                Col = iCell.Offset(-lRows, 0).Row
                '            Col = Right(iCell.Offset(-lRows, 0).Address, 2)
                Call SplitCells
            End If
        Next
        Columns(25).ClearContents
        Call origcolQ
        Call firstpagecolQ
        Application.ScreenUpdating = True
        Call msgbox1
     
    End Sub
    Let me know of issues.

  9. #9
    Registered User
    Join Date
    07-07-2011
    Location
    tx
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Comma delimited list cell to new row code help

    That works!! Thank you SO MUCH!!! You really saved my butt.

    Thank you again,
    John

  10. #10
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Comma delimited list cell to new row code help

    You're welcome John...glad I could help.

  11. #11
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Comma delimited list cell to new row code help

    NumberOfCommas = Len(aString) - Len(Replace(aString, ",", vbNullString))

  12. #12
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Comma delimited list cell to new row code help

    @mikerickson

    Of course you're right...since I was rewriting that particular part of the code, I should have rewritten it to make it more efficient. Not sure why I didn't...I knew there were better ways. Still learning myself to lead OP's to better solutions...want them to know "what they have works"...but, try this. It's a balance...working on it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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