+ Reply to Thread
Results 1 to 4 of 4

How to separate cell values and insert into new rows

Hybrid View

  1. #1
    Registered User
    Join Date
    12-11-2012
    Location
    Charlotte, NC
    MS-Off Ver
    Excel 2007
    Posts
    11

    How to separate cell values and insert into new rows

    Hi all,
    I am looking for a macro to look at a colum with a cell that has values that are separated by a comma or a space or a carriage return and make new rows with only a single value for that column of cells. I am sorry that sounds confusing but I have attached a sample worksheet. In the worksheet, I am looking at Column B and want to separate them into different lines but copy Column A and Column C for the new line. So I did the first one, which is row 2 was separated to row 3 and row 4. Any help on this would be great.

  2. #2
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: How to separate cell values and insert into new rows

    Hi,

    click the button on the first sheet and check result on 2nd sheet: test_multiple_cells.xlsm


    contains:
    Option Explicit
    
    Private Sub CommandButton1_Click()
        Application.ScreenUpdating = False
        RestrucData
        Application.ScreenUpdating = True
    End Sub
    
    Sub RestrucData()
        Dim dicData As Object: Set dicData = CreateObject("Scripting.Dictionary")
        Dim dicChild As Object
        Dim aData, sValue
        Dim lngMaxRows As Long, i As Long, j As Integer
        
        With ThisWorkbook.Worksheets("Sheet1") 'adapt sheetname
            lngMaxRows = .Cells(.Rows.Count, 1).End(xlUp).Row
            For i = 2 To lngMaxRows
            
                sValue = .Cells(i, 2).Value
                If InStr(sValue, vbLf) > 0 Then 'return
                    aData = Split(sValue, vbLf)
                ElseIf InStr(sValue, ",") > 0 Then 'comma
                    aData = Split(sValue, ",")
                ElseIf InStr(sValue, " ") > 0 Then 'space
                    aData = Split(sValue, " ")
                End If
                
                Set dicChild = CreateObject("Scripting.Dictionary")
                If IsArrayAllocated(aData) Then
                    For j = LBound(aData) To UBound(aData)
                        dicChild(j) = Array(.Cells(i, 1).Value, Trim(aData(j)), .Cells(i, 3).Value)
                    Next j
                    Erase aData
                Else
                    dicChild(0) = Array(.Cells(i, 1).Value, .Cells(i, 2).Value, .Cells(i, 3).Value)
                End If
                dicData.Add i, dicChild
            Next i
        End With
            
        With ThisWorkbook.Worksheets("Sheet2") 'adapt sheetname
            lngMaxRows = .Cells(.Rows.Count, 1).End(xlUp).Row
            For Each aData In dicData.Keys
                For Each sValue In dicData(aData).Keys
                    .Range(.Cells(lngMaxRows, 1), .Cells(lngMaxRows, 3)).Value = dicData(aData)(sValue)
                    lngMaxRows = lngMaxRows + 1
                Next sValue
            Next aData
            .Activate
        End With
            
    End Sub
    
    Function IsArrayAllocated(arr) As Boolean
        On Error GoTo ErrHandler
        Dim tmpVal
        tmpVal = LBound(arr)
        IsArrayAllocated = True
        Exit Function
    ErrHandler:
        IsArrayAllocated = False
    End Function
    Please use [CODE]-TAGS
    When your problem is solved mark the thread SOLVED
    If an answer has helped you please click to give reputation
    Read the FORUM RULES

  3. #3
    Valued Forum Contributor
    Join Date
    11-20-2012
    Location
    Seattle, WA USA
    MS-Off Ver
    Excel 2010
    Posts
    597

    Re: How to separate cell values and insert into new rows

    this works based on your test_multiple_cells.xlsx
    run this macro

    Function NthWord(Words As String, N As Integer) As String
       'Extracts the Nth word from the string Words
       Dim WordArray     As Variant
       WordArray = Split(Words)
       If UBound(WordArray) < N - 1 Then
          NthWord = "#N/A"
       Else
          NthWord = WordArray(N - 1)
       End If
    End Function
    
    
    Sub Loop_Example()
        Dim Firstrow As Long
        Dim Lastrow As Long
        Dim Lrow As Long
    
        With ActiveSheet
    
            Firstrow = 2
            Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
    
            For Lrow = Lastrow To Firstrow Step -1
    
                With .Cells(Lrow, "B")
    
                    If Not IsError(.Value) Then
    
                        If .Value Like "*, *" Then
                            x = Range("B" & Lrow)
                            Range("B" & Lrow).Value = Replace(x, ", ", Chr(32))
                        End If
                        
                        If .Value Like "* *" Then
                            x = Range("B" & Lrow)
                            Range("B" & Lrow).Value = Replace(x, " ", Chr(32))
                        End If
                        
                        b = Chr(10)
                        c = " "
                        Range("B:B").Replace What:=b, Replacement:=c, LookAt:=xlPart, SearchOrder _
                            :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
                    
                    End If
    
                End With
    
            Next Lrow
            
            For Lrow = Lastrow To Firstrow Step -1
    
                With .Cells(Lrow, "B")
    
                    If Not IsError(.Value) Then
    
                        x = Range("B" & Lrow)
     
                        Ln = Len(x) - Len(Replace(x, " ", ""))
                        If Ln > 0 Then
                            Range(Range("B" & Lrow + 1), Range("B" & Lrow + Ln)).EntireRow.Insert (2)
                        Else:
                        End If
                        For i = Lrow + 1 To Lrow + Ln
                        x = Range("B" & Lrow)
                            Range("A" & i).FillDown
                            Range("C" & i).FillDown
                            theword = NthWord(Range("B" & Lrow).Value, 2)
                            Range("B" & i).Value = theword
                            Range("B" & Lrow).Value = Replace(x, " " & theword, "")
                        Next
                        End If
    
                End With
    
            Next Lrow
            
           
    
        End With
    
    End Sub

  4. #4
    Registered User
    Join Date
    12-11-2012
    Location
    Charlotte, NC
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: How to separate cell values and insert into new rows

    Thanks guys,
    Both work well, but I like Scott's since it fixes it on the same sheet. But both were awesome. Much thanks!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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