Convert Horizontal Data to Vertical

twilson4

New Member
Joined
Jul 27, 2015
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a long string of data that is laid out horizontally and I am trying to find a way (without VBA) to relatively easily convert it to be vertically stacked. Below is a small example of what I need to be able to do on a much larger scale.

If I set up the horizontal spreadsheet to have the 6 rows for each 1 row of data on the horizontal, is there a formula that allows me to look up a combination of styles and types from the vertical table and grab them from both the column and row in the horizontal table?

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][TABLE="width: 519"]
<colgroup><col><col><col><col><col><col><col><col><col></colgroup><tbody>[TR]
[TD]Department[/TD]
[TD]Category[/TD]
[TD]Style[/TD]
[TD]Jan Plan[/TD]
[TD]Jan Act[/TD]
[TD]Jan Var[/TD]
[TD]Feb Plan[/TD]
[TD]Feb Act[/TD]
[TD]Feb Var[/TD]
[/TR]
[TR]
[TD]Men[/TD]
[TD]Outerwear[/TD]
[TD]ABC[/TD]
[TD="align: right"]200[/TD]
[TD="align: right"]250[/TD]
[TD="align: right"]50[/TD]
[TD="align: right"]200[/TD]
[TD="align: right"]250[/TD]
[TD="align: right"]50[/TD]
[/TR]
[TR]
[TD]Men[/TD]
[TD]Outerwear[/TD]
[TD]DEF[/TD]
[TD="align: right"]300[/TD]
[TD="align: right"]250[/TD]
[TD="align: right"]-50[/TD]
[TD="align: right"]300[/TD]
[TD="align: right"]250[/TD]
[TD="align: right"]-50[/TD]
[/TR]
[TR]
[TD]Men[/TD]
[TD]Outerwear[/TD]
[TD]GHI[/TD]
[TD="align: right"]400[/TD]
[TD="align: right"]350[/TD]
[TD="align: right"]-50[/TD]
[TD="align: right"]400[/TD]
[TD="align: right"]350[/TD]
[TD="align: right"]-50[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Department[/TD]
[TD]Category[/TD]
[TD]Style[/TD]
[TD]Type[/TD]
[TD]Value[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Men[/TD]
[TD]Outerwear[/TD]
[TD]ABC[/TD]
[TD]Jan Plan[/TD]
[TD="align: right"]200[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Men[/TD]
[TD]Outerwear[/TD]
[TD]ABC[/TD]
[TD]Jan Act[/TD]
[TD="align: right"]250[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Men[/TD]
[TD]Outerwear[/TD]
[TD]ABC[/TD]
[TD]Jan Var[/TD]
[TD="align: right"]50[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Men[/TD]
[TD]Outerwear[/TD]
[TD]ABC[/TD]
[TD]Feb Plan[/TD]
[TD="align: right"]200[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Men[/TD]
[TD]Outerwear[/TD]
[TD]ABC[/TD]
[TD]Feb Act[/TD]
[TD="align: right"]250[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Men[/TD]
[TD]Outerwear[/TD]
[TD]ABC[/TD]
[TD]Feb Var[/TD]
[TD="align: right"]50[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Men[/TD]
[TD]Outerwear[/TD]
[TD]DEF[/TD]
[TD]Jan Plan[/TD]
[TD="align: right"]300[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Men[/TD]
[TD]Outerwear[/TD]
[TD]DEF[/TD]
[TD]Jan Act[/TD]
[TD="align: right"]250[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Men[/TD]
[TD]Outerwear[/TD]
[TD]DEF[/TD]
[TD]Jan Var[/TD]
[TD="align: right"]-50[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Men[/TD]
[TD]Outerwear[/TD]
[TD]DEF[/TD]
[TD]Feb Plan[/TD]
[TD="align: right"]300[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Men[/TD]
[TD]Outerwear[/TD]
[TD]DEF[/TD]
[TD]Feb Act[/TD]
[TD="align: right"]250[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Men[/TD]
[TD]Outerwear[/TD]
[TD]DEF[/TD]
[TD]Feb Var[/TD]
[TD="align: right"]-50[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Men[/TD]
[TD]Outerwear[/TD]
[TD]GHI[/TD]
[TD]Jan Plan[/TD]
[TD="align: right"]400[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Men[/TD]
[TD]Outerwear[/TD]
[TD]GHI[/TD]
[TD]Jan Act[/TD]
[TD="align: right"]350[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Men[/TD]
[TD]Outerwear[/TD]
[TD]GHI[/TD]
[TD]Jan Var[/TD]
[TD="align: right"]-50[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Men[/TD]
[TD]Outerwear[/TD]
[TD]GHI[/TD]
[TD]Feb Plan[/TD]
[TD="align: right"]400[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Men[/TD]
[TD]Outerwear[/TD]
[TD]GHI[/TD]
[TD]Feb Act[/TD]
[TD="align: right"]350[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Men[/TD]
[TD]Outerwear[/TD]
[TD]GHI[/TD]
[TD]Feb Var[/TD]
[TD="align: right"]-50[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
twilson4,

I know that you asked for a formula solution, but, none of the formula Guru's have responded yet.

Here is a macro solution for you to consider that is based on your two flat text displays, that uses two arrays in memory, and, will adjust to the number of raw data rows, and, columns.

I assume that your raw data is in worksheet Sheet1, and, that the results will be written to worksheet Results.

I assume that both worksheets already exist, and, you can change the worksheet names in the macro.

Your raw data is in worksheet Sheet1, range A1:I4.

The results will be in worksheet Results, range A1:E19.


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).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub Convert_Horizontal_Data_to_Vertical()
' hiker95, 09/08/2017, ME1022065
Application.ScreenUpdating = False
Dim w1 As Worksheet, wr As Worksheet
Dim a As Variant, lr As Long, lc As Long, n As Long, i As Long, c As Long
Dim o As Variant, j As Long
Set w1 = Sheets("Sheet1")   '<-- you can change the sheet name here
Set wr = Sheets("Results")  '<-- you can change the sheet name here
With w1
  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 = Application.Count(.Range(.Cells(2, 4), .Cells(lr, lc)))
  ReDim o(1 To n + 1, 1 To 5)
End With
j = j + 1: o(j, 1) = "Department": o(j, 2) = "Category"
o(j, 3) = "Style": o(j, 4) = "Type": o(j, 5) = "Value"
For i = 2 To UBound(a, 1)
  For c = 4 To UBound(a, 2)
    j = j + 1: o(j, 1) = a(i, 1): o(j, 2) = a(i, 2): o(j, 3) = a(i, 3)
    o(j, 4) = a(1, c): o(j, 5) = a(i, c)
  Next c
Next i
With wr
  .UsedRange.ClearContents
  .Range("A1").Resize(UBound(o, 1), UBound(o, 2)) = o
  .UsedRange.Columns.AutoFit
  .Activate
End With
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the Convert_Horizontal_Data_to_Vertical macro.
 
Upvote 0
I will give the macro a try...my data is significantly larger than the example I gave so will see if I can get it to work.

To the responses asking if it was a Transpose question...yes it is on it's most basic need. However, I need it to transpose the columns to rows while duplicating what is already in the rows. All the while my data will refresh periodically making it something I am trying to get set up through formulas so it can happen automatically.

Appreciate everyone's time and assistance...
 
Upvote 0
I will give the macro a try...my data is significantly larger than the example I gave so will see if I can get it to work.

twilson4,

Make sure that you try the macro on a copy of your workbook.
 
Upvote 0
twilson4,

Make sure that you try the macro on a copy of your workbook.

I ended up getting an Index Match Match formula to work using this "how to"...

http://www.mbaexcel.com/excel/how-to-use-index-match-match/

[FONT=&quot]= INDEX ( [/FONT][FONT=&quot]array[/FONT][FONT=&quot] , MATCH ( [/FONT][FONT=&quot]lookup_value[/FONT][FONT=&quot] , [/FONT][FONT=&quot]lookup_array[/FONT][FONT=&quot] , 0 ) , MATCH ( [/FONT][FONT=&quot]lookup_value[/FONT][FONT=&quot] , [/FONT][FONT=&quot]lookup_array[/FONT][FONT=&quot] , 0 ) )


[/FONT]
Next question...does anyone know how to reference cells in sequential orders...ie( a1, a2, a3...) when referencing them in b1, b10, b20, b30...? I would like to be able to copy paste down in column B but because it requires a gap instead of going to a2, it references a10.

Is this possible?
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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