Forum Discussion

Tami_E's avatar
Tami_E
Copper Contributor
Sep 08, 2022
Solved

Need Help with turning columns into rows

I have a table of workers and for each a set of columns that indicate information that needs to be moved to a linear format.   I changed the workers to fruits to make it easier to understand, but t...
  • OliverScheurich's avatar
    Sep 08, 2022

    Tami_E 

    Sub worker()
    
    Dim i As Long
    Dim j As Long
    Dim m As Long
    Dim k As Long
    Dim l As Long
    Dim n As Long
    
    Range(Cells(15, 1), Cells(1048576, 5)).Clear
    
    i = Range("A" & Rows.Count).End(xlUp).Row
    m = 15
    
    
    For j = 2 To i
    
    Range(Cells(m, 1), Cells(m + 2, 1)).Value = Cells(j, 1).Value
    Range(Cells(m, 2), Cells(m, 5)).Value = Range(Cells(j, 2), Cells(j, 5)).Value
    Range(Cells(m + 1, 2), Cells(m + 1, 5)).Value = Range(Cells(j, 6), Cells(j, 9)).Value
    Range(Cells(m + 2, 2), Cells(m + 2, 5)).Value = Range(Cells(j, 10), Cells(j, 13)).Value
    
    m = m + 3
    
    Next j
    
    
    l = Range("A" & Rows.Count).End(xlUp).Row
    
    For n = 15 To l
    
    If IsEmpty(Cells(n, 2)) And Not (IsEmpty(Cells(n, 1))) Then
    Range(Cells(n, 1), Cells(n, 5)).Delete
    n = n - 1
    
    
    Else
    End If
    
    Next n
    
    End Sub

    Maybe with these lines of code. In the attached file you can click the button in cell F10 to run the macro. If the table with original data is large (e.g. 1000 rows) the result table can be in columns P to T for example.

Resources