Results 1 to 10 of 35

Thread: Redistribute a Delimited Column Of Data into Separate Rows (Keeping Other Data As Is)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0
    Hi Rick,

    Excellent code, I must say, but I found a problem if, for any reason, a customer has the column C blank.

    Something like (Joe has no Parts)

    Name Client Parts
    Rick 1111111 P1, P2, P3
    Sam 2222222 P2, P5
    Joe 3333333
    Bill 4444444 P4, P6, P7, P8


    This line of code throws an error

    Code:
      Intersect (Rows (X + 1), Columns (TableColumns)). Resize (UBound (Data)). XlShiftDown Insert

    The solution I found is (modifications in blue)

    Code:
    Sub RedistributeDataV2()
      Dim X As Long, LastRow As Long, A As Range, Table As Range, Data() As String
      Const Delimiter As String = ", "
      Const DelimitedColumn As String = "C"
      Const TableColumns As String = "A:C"
      Const StartRow As Long = 2
      Application.ScreenUpdating = False
      LastRow = Columns(TableColumns).Find(What:="*", SearchOrder:=xlRows, _
                SearchDirection:=xlPrevious, LookIn:=xlFormulas).Row
      For X = LastRow To StartRow Step -1
        Data = Split(Cells(X, DelimitedColumn), Delimiter)
            
       If UBound(Data) And UBound(Data) <> -1 Then
          Intersect(Rows(X + 1), Columns(TableColumns)).Resize(UBound(Data)).Insert xlShiftDown
          Cells(X, DelimitedColumn).Resize(UBound(Data) + 1) = WorksheetFunction.Transpose(Data)
        End If
      
      Next
      LastRow = Cells(Rows.Count, DelimitedColumn).End(xlUp).Row
      
      On Error GoTo NoBlanks
      Set Table = Intersect(Columns(TableColumns), Rows(StartRow) _
        .Resize(LastRow - StartRow + 1, Columns(TableColumns).Columns.Count - 1))  
     On Error GoTo 0
    
      For Each A In Table.SpecialCells(xlBlanks).Areas
            A.FormulaR1C1 = "=R[-1]C"
            A.Value = A.Value
      Next
    NoBlanks:
      Application.ScreenUpdating = True
    End Sub

    This is reasonable or is there another more efficient solution?

    Best Regards,

    Marcelo

  2. #2
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    15
    Quote Originally Posted by Marcelo Branco View Post
    Hi Rick,

    Excellent code, I must say, but I found a problem if, for any reason, a customer has the column C blank.

    Something like (Joe has no Parts)

    Name Client Parts
    Rick 1111111 P1, P2, P3
    Sam 2222222 P2, P5
    Joe 3333333
    Bill 4444444 P4, P6, P7, P8

    This line of code throws an error

    Code:
      Intersect (Rows (X + 1), Columns (TableColumns)). Resize (UBound (Data)). XlShiftDown Insert
    Thank you so much for pointing out this problem to me... I cannot believe it did not occur to me to test data where one or more of the cells are emptly. And thank you for your code suggestion as well; however, I decided to take a different route in fixing the problem. Your alert to me allowed me to review my code with "fresh eyes" and I saw a way to correct the problem while at the same time tighten up the code somewhat (I managed to eliminate the last For..Next loop entirely). I replaced the original code in Message #1 with my new code and put a note at the top indicating that message contains revised code (for those who previously visited this thread and then re-visit it in the future). Thank you again for pointing out the problem to me... I really appreciated it.

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0
    Rick,

    I'm glad for, somehow, have contributed to a better solution.

    New code copied for future use.

    Regards,

    M.

  4. #4
    Member
    Join Date
    Dec 2012
    Posts
    43
    Rep Power
    0
    very nice. i was looking for this code. coincidentally, i searched before posting the question in Mrexcel forum and here i got exactly what i needed.

    thank you Rick and Marcelo.
    Last edited by Excel Fox; 08-18-2013 at 08:39 PM. Reason: Quote removed as it didn't add value to the post

  5. #5
    Junior Member
    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0
    Nice piece of code, although I see a problem, when a value is blank in another collumn, it is copied from the cell that contains a value, so it filles other empty cells with bogus data

    Eg.

    AA BBB 1:2
    CC 2:3

    Will become:

    AA BBB 1
    AA BBB 2
    CC BBB 2
    CC BBB 3

    Any solution for this ?

Similar Threads

  1. Move data from rows into columns for every unique value
    By mahmoud-lee in forum Excel Help
    Replies: 4
    Last Post: 06-13-2013, 03:02 AM
  2. Replies: 4
    Last Post: 05-01-2013, 09:49 PM
  3. Replies: 3
    Last Post: 03-16-2013, 05:13 PM
  4. Replies: 12
    Last Post: 08-19-2012, 06:17 PM
  5. Transpose data into Rows
    By vikash200418 in forum Excel Help
    Replies: 2
    Last Post: 04-10-2012, 11:02 PM

Posting Permissions

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