Convert Rows to Columns

BruceHawk

New Member
Joined
Mar 20, 2016
Messages
12
I thought someone would have asked this, and they may have, but I was not successful in finding the answer in Excel.

I have a large spreadsheet in rows that I would like to copy and convert to columns.

Example of what I have:

LocationData 1Data 2Data 3
Origin 1Option 1Alt 2Other 1
Origin 2Option 1Alt 2Other 1
Origin 3Option 2Alt 4Other 1
Origin 4Option 3Alt 4Other 1
Origin 5Option 3Alt 5Other 2

<colgroup><col width="64" span="4" style="width:48pt"> </colgroup><tbody>
</tbody>

What I would like to convert it to:

LocationData
Origin 1Option 1
Alt 2
Other 1
Origin 2Option 1
Alt 2
Other 1
Origin 3Option 2
Alt 4
Other 1
Origin 4Option 3
Alt 4
Other 1
Origin 5Option 3
Alt 5
Other 2

<colgroup><col width="64" span="2" style="width:48pt"> </colgroup><tbody>
</tbody>

Help! Thanks!!
 
BruceHawk,

Here is another macro solution for you to consider, that is based on your latest flat text display in your reply #9, that uses two arrays in memory, and, will adjust to the number of raw data rows, and, columns, in the active worksheet.

The results will be written in the active worksheet, beginning, in the third column to the right of the last used raw data column.

With your raw data in range A1:D6, the results will be written to range G1:H14.

Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub ReorganizeData_V2()
' hiker95, 08/26/2017, ME1017714
Application.ScreenUpdating = False
Dim a As Variant, i As Long, c As Long, lr As Long, lc As Long, n As Long
Dim o As Variant, j As Long
With ActiveSheet
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  lc = .Cells(1, Columns.Count).End(xlToLeft).Column
  a = .Range(.Cells(1, 1), .Cells(lr, lc))
  n = (lr - 1) * (lc - 1)
  ReDim o(1 To n + 1, 1 To 2)
  j = j + 1: o(j, 1) = "Location": o(j, 2) = "Data"
  For i = 2 To UBound(a, 1)
    For c = 2 To UBound(a, 2)
      If Not a(i, c) = vbEmpty Then
        j = j + 1: o(j, 1) = a(i, 1): o(j, 2) = a(i, c)
      End If
    Next c
  Next i
  .Cells(1, lc + 3).Resize(UBound(o, 1), UBound(o, 2)) = o
  lr = .Cells(Rows.Count, lc + 3).End(xlUp).Row
  For i = 2 To lr
    n = Application.CountIf(.Columns(lc + 3), .Cells(i, lc + 3).Value)
    If n > 1 Then
      .Cells(i + 1, lc + 3).Resize(n - 1).ClearContents
      i = i + n - 1
    End If
  Next i
  .Columns(lc + 3).Resize(, 2).AutoFit
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
BruceHawk,

Here is another macro solution for you to consider, that is based on your latest flat text display in your reply #9, that uses two arrays in memory, and, will adjust to the number of raw data rows, and, columns, in the active worksheet.

The results will be written in the active worksheet, beginning, in the third column to the right of the last used raw data column.

With your raw data in range A1:D6, the results will be written to range G1:H16, and, will also display the blank cells.


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


Code:
Sub ReorganizeData_WithBlanks()
' hiker95, 08/27/2017, ME1017714
Application.ScreenUpdating = False
Dim a As Variant, i As Long, c As Long, lr As Long, lc As Long, n As Long
Dim o As Variant, j As Long
With ActiveSheet
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  lc = .Cells(1, Columns.Count).End(xlToLeft).Column
  a = .Range(.Cells(1, 1), .Cells(lr, lc))
  n = (lr - 1) * (lc - 1)
  ReDim o(1 To n + 1, 1 To 2)
  j = j + 1: o(j, 1) = "Location": o(j, 2) = "Data"
  For i = 2 To UBound(a, 1)
    For c = 2 To UBound(a, 2)
      If c = 2 Then
        j = j + 1: o(j, 1) = a(i, 1): o(j, 2) = a(i, c)
      Else
        j = j + 1: o(j, 2) = a(i, c)
      End If
    Next c
  Next i
  .Cells(1, lc + 3).Resize(UBound(o, 1), UBound(o, 2)) = o
  .Columns(lc + 3).Resize(, 2).AutoFit
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,093
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top