Transpose

kumark1

New Member
Joined
Mar 20, 2012
Messages
11
Below is my input.


PROD AT1 DESC AT2 DESC AT3 DESC AT4 DESC
fan 1 at 2 sd 6 tf 6 hg
book 9 td 3 hg 4 jh
Look 7 kj 5 ih

OUTPUT SHOULD BE

PROD AT1 DESC
FAN 1 at
FAN 2 sd
FAN 6 tf
FAN 6 hg
book 9 td
book 3 hg
book 4 jh
look 7 kj
look 5 ih



This Should be the output after i run the MACRO

Rows should be Transposed into Columns
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
kumark1,

Welcome to the MrExcel forum.


Sample raw data in Sheet1:


Excel Workbook
ABCDEFGHI
1PRODAT1DESCAT2DESCAT3DESCAT4DESC
2fan1at2sd6tf6hg
3book9td3hg4jh
4Look7kj5ih
5
Sheet1





After the macro in a new worksheet Results:


Excel Workbook
ABC
1PRODAT1DESC
2fan1at
3fan2sd
4fan6tf
5fan6hg
6book9td
7book3hg
8book4jh
9Look7kj
10Look5ih
11
Results





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, by highlighting the code and pressing the keys CTRL + C
2. Open your 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 by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub ReorgData()
' hiker95, 03/20/2012
' http://www.mrexcel.com/forum/showthread.php?t=622550
Dim w1 As Worksheet, wR As Worksheet
Dim r As Long, lr As Long, c As Long, lc As Long, nr As Long, n As Long, rr As Long
Application.ScreenUpdating = False
Set w1 = Worksheets("Sheet1")
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"
Set wR = Worksheets("Results")
wR.UsedRange.Clear
wR.Cells(1, 1).Resize(, 3).Value = w1.Cells(1, 1).Resize(, 3).Value
lr = w1.Cells(Rows.Count, 1).End(xlUp).Row
For r = 2 To lr Step 1
  nr = wR.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
  lc = w1.Cells(r, Columns.Count).End(xlToLeft).Column
  n = (lc - 1) / 2
  wR.Cells(nr, 1).Resize(n).Value = w1.Cells(r, 1).Value
  rr = nr
  For c = 2 To lc Step 2
    wR.Cells(rr, 2).Resize(, 2).Value = w1.Cells(r, c).Resize(, 2).Value
    rr = rr + 1
  Next c
Next r
wR.UsedRange.Columns.AutoFit
wR.Activate
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


Then run the ReorgData macro.
 
Upvote 0

Forum statistics

Threads
1,216,116
Messages
6,128,932
Members
449,480
Latest member
yesitisasport

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