Consulting

Results 1 to 11 of 11

Thread: Split Cells into rows but with two columns

  1. #1
    VBAX Regular
    Joined
    Jan 2018
    Location
    Nova Scotia
    Posts
    83
    Location

    Split Cells into rows but with two columns

    Hi All,
    I am trying to split cells into rows but I have two columns to review. When I try to split the cells, they don't line up. I found some code to get me going but unfortunately I'm struggling getting it working.

    Objective: Take cells from Column C and Column E and split the cells into rows. For example, the data could be "A04, A05" (Column C) and "A07" (Column E) . I want a new inserted row(s) so that the table copies the row down (Col A, Col B, etc) except Col C would be A04 and A05 with the corresponding A07 from Col E.

    I'm attaching the sample file with dummy data. Here is the code:

    Sub Splt()
    Dim LR As Long, i As Long
    Dim X As Variant
    Application.ScreenUpdating = False
    LR = Range("C" & Rows.Count).End(xlUp).Row
    
    Columns("C").Insert
    For i = LR To 1 Step -1
        With Range("D" & i)
            If InStr(.Value, ",") = 0 Then
                .Offset(, 0).Value = .Value
            Else
                X = Split(.Value, ",")
                .Offset(1).Resize(UBound(X)).EntireRow.Insert
                .Offset(, 0).Resize(UBound(X) - LBound(X) + 1).Value = Application.Transpose(X)
            End If
        End With
    Next i
    Columns("C").Delete
    
    Columns("E").Insert
    For i = LR To 1 Step -1
        With Range("F" & i)
            If InStr(.Value, ",") = 0 Then
                .Offset(, 0).Value = .Value
            Else
                X = Split(.Value, ",")
                .Offset(1).Resize(UBound(X)).EntireRow.Insert
                .Offset(, 0).Resize(UBound(X) - LBound(X) + 1).Value = Application.Transpose(X)
            End If
        End With
    Next i
    Columns("E").Delete
    
    
    'Columns("B").Delete
    'LR = Range("A" & Rows.Count).End(xlUp).Row
    'With Range("B1:C" & LR)
    '    On Error Resume Next
    '    .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
    '    On Error GoTo 0
    '    .Value = .Value
    'End With
    Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    Last edited by Paul_Hossler; 02-05-2018 at 09:33 AM. Reason: Added CODE tags

  2. #2
    VBAX Regular
    Joined
    Jan 2018
    Location
    Nova Scotia
    Posts
    83
    Location
    I added another file with a table (starting at P1) to show exactly the desired result from the table starting in A1
    Attached Files Attached Files

  3. #3
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    I think this will do what you want.
    Sub test()
        Dim SourceRange As Range, DestinationRange As Range
        Dim i As Long, maxRow As Long, cellParts As Variant, splitSize As Long
        Set SourceRange = Range("A1")
        Set DestinationRange = Range("k1")
        
        DestinationRange.EntireColumn.ClearContents
        With SourceRange
            With Range(.Cells(1, 1), .Cells(Rows.Count, 1).End(xlUp)).Resize(, 6)
                Set SourceRange = .Cells
                Set DestinationRange = DestinationRange.Resize(.Rows.Count, 6)
            End With
        End With
        
        With DestinationRange
            .EntireColumn.ClearContents
            .Value = SourceRange.Value
            maxRow = .Rows.Count
        End With
        i = 3
        Do
            With DestinationRange.Rows(i)
              
                cellParts = Split(CStr(.Cells(1, 3).Value), ", ")
                splitSize = UBound(cellParts)
                If splitSize < 1 Then
                    cellParts = Split(CStr(.Cells(1, 5).Value), ", ")
                    splitSize = UBound(cellParts)
                    If splitSize < 1 Then
                        i = i + 1
                    Else
                        .Offset(1, 0).Resize(splitSize, .Columns.Count).Insert
                        .Resize(splitSize + 1, .Columns.Count).FillDown
                        .Cells(1, 5).Resize(splitSize + 1, 1).Value = Application.Transpose(cellParts)
                        maxRow = maxRow + splitSize
                    End If
                Else
                    .Offset(1, 0).Resize(splitSize, .Columns.Count).Insert
                    .Resize(splitSize + 1, .Columns.Count).FillDown
                    .Cells(1, 3).Resize(splitSize + 1, 1).Value = Application.Transpose(cellParts)
                    maxRow = maxRow + splitSize
                End If
            End With
        Loop Until maxRow < i
    End Sub

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,872
    or try this directly on (a copy of) your PERT CHART sheet:
    Sub Splt()
    'Macro2
    Dim LR As Long, i As Long
    Dim X As Variant
    Application.ScreenUpdating = False
    LR = Range("C" & Rows.Count).End(xlUp).Row
    
    For i = LR To 1 Step -1
      With Range("C" & i)
        If InStr(.Value, ",") > 0 Then
          X = Split(.Value, ",")
          With Range("A" & i).Resize(, 6)
            .Copy
            .Offset(1).Resize(UBound(X)).Insert Shift:=xlDown
            .Offset(1).Resize(UBound(X)).Value = .Value
          End With
          .Offset(, 0).Resize(UBound(X) - LBound(X) + 1).Value = Application.Transpose(X)
        End If
      End With
    Next i
    
    LR = Range("C" & Rows.Count).End(xlUp).Row
    
    For i = LR To 1 Step -1
      With Range("E" & i)
        If InStr(.Value, ",") > 0 Then
          X = Split(.Value, ",")
          With Range("A" & i).Resize(, 6)
            .Copy
            .Offset(1).Resize(UBound(X)).Insert Shift:=xlDown
            .Offset(1).Resize(UBound(X)).Value = .Value
          End With
          .Offset(, 0).Resize(UBound(X) - LBound(X) + 1).Value = Application.Transpose(X)
        End If
      End With
    Next i
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    End Sub
    It inserts rows in columns A to F.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    VBAX Regular
    Joined
    Jan 2018
    Location
    Nova Scotia
    Posts
    83
    Location
    Hi p45cal,

    I think I got most of it working with the exception of one small problem. When an activity code has 2 predecessors, it works ok, but when it has 3 predecessors, it doesn't repeat or copy down...the cells in column A and B are blank. For example, when I click the macro "SPLT", A18 and A22 doesn't fill in or repeat the activity code and description. The predecessors are transposed down just like I want it to but Column A and Column values do not fill in.

    Here's my simple code and attached file (the macro button is on the first tab and I want it to look like the last tab)

    Sub Splt()
    Dim LR As Long, i As Long
    Dim X As Variant
    Dim Y As Variant
    Dim Z As Variant
    'Application.ScreenUpdating = False
    LR = Range("C" & Rows.Count).End(xlUp).Row
    For i = LR To 1 Step -1
    With Range("C" & i)
    If InStr(.Value, ",") = 0 Then
    .Offset(, 0).Value = .Value
    Else
    X = Split(.Value, ",")
    .Offset(1).Resize(UBound(X)).EntireRow.Insert
    .Offset(, 0).Resize(UBound(X) - LBound(X) + 1).Value = Application.Transpose(X)
    With Range("A" & i)
    Y = Cells(i, 1).Value
    .Offset(1, 0).Value = .Value
    End With
    With Range("B" & i)
    Z = Cells(i, 1).Value
    .Offset(1, 0).Value = .Value
    End With
    End If
    End With

    Next i
    'Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files

  6. #6
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,872
    Why ask for help then completely ignore it?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  7. #7
    VBAX Regular
    Joined
    Jan 2018
    Location
    Nova Scotia
    Posts
    83
    Location
    Sorry. I didn't ignore it.
    i should've said that I tried your suggested code but it was erroring.
    Then, after further review, I didn't need to transpose column C.
    My requirements changed. I went back to my code and realized I only needed half of it.
    As a result, I thought it was best to re-explain what I was trying to do which is replicate or repeat columns A and B.

    can you help me please?

  8. #8
    VBAX Regular
    Joined
    Jan 2018
    Location
    Nova Scotia
    Posts
    83
    Location
    I meant Column E. I didn't need to transpose Column E. I only wanted Column C to transpose.
    My problem is with Column A and B not filling in.

  9. #9
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,872
    See attached.
    Quote Originally Posted by Sully1440 View Post
    I went back to my code and realized I only needed half of it.
    Then remove half my code (although it doesn't make any difference to the result on your sample sheet from my original code):
    Sub SpltB()
    Dim LR As Long, i As Long
    Dim X As Variant
    Application.ScreenUpdating = False
    LR = Range("C" & Rows.Count).End(xlUp).Row
    
    For i = LR To 1 Step -1
      With Range("C" & i)
        If InStr(.Value, ",") > 0 Then
          X = Split(.Value, ",")
          With Range("A" & i).Resize(, 6)
            .Copy
            .Offset(1).Resize(UBound(X)).Insert Shift:=xlDown
            .Offset(1).Resize(UBound(X)).Value = .Value
          End With
          .Offset(, 0).Resize(UBound(X) - LBound(X) + 1).Value = Application.Transpose(X)
        End If
      End With
    Next i
    
    'LR = Range("C" & Rows.Count).End(xlUp).Row
    '
    'For i = LR To 1 Step -1
    '  With Range("E" & i)
    '    If InStr(.Value, ",") > 0 Then
    '      X = Split(.Value, ",")
    '      With Range("A" & i).Resize(, 6)
    '        .Copy
    '        .Offset(1).Resize(UBound(X)).Insert Shift:=xlDown
    '        .Offset(1).Resize(UBound(X)).Value = .Value
    '      End With
    '      .Offset(, 0).Resize(UBound(X) - LBound(X) + 1).Value = Application.Transpose(X)
    '    End If
    '  End With
    'Next i
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  10. #10
    VBAX Regular
    Joined
    Jan 2018
    Location
    Nova Scotia
    Posts
    83
    Location
    I am such a boob. I just noticed that you replied with a second message with my code. I am sorry!!!

    I'm looking at it now.

  11. #11
    VBAX Regular
    Joined
    Jan 2018
    Location
    Nova Scotia
    Posts
    83
    Location
    AWESOME.....It works perfectly. THANK YOU, THANK YOU, THANK YOU
    I like the SpltB version.


Posting Permissions

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