Transpose Data

VinodKrishnappa

New Member
Joined
Aug 6, 2016
Messages
31
Hi Can any one help me on this transpose the available following data in a particular format. This data will continue for more than 650 rows & 26 columns

Emp. No.
NameComp1Comp2Comp3Comp4Comp5Comp8Comp9
2002A2000800160025001250001500
2003B2200880160025001250001500
2004C240096016002500120002001500

<colgroup><col><col><col span="7"></colgroup><tbody>
</tbody>

Note: Any Comp with 0 value to be ignored in the transposed data.

Format for transpose

Comps
Amt.Emp. ID
Comp120002002
Comp28002002
Comp316002002
Comp425002002
Comp5125002002
Comp915002002
Comp122002003
Comp28802003
Comp316002003
Comp425002003
Comp5125002003
Comp915002003
Comp124002004
Comp2
9602004
Comp316002004
Comp425002004
Comp5120002004
Comp82002004
Comp915002004

<colgroup><col span="3"></colgroup><tbody>
</tbody>

Please help me on this.

Thanks in advance.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Could do it with VBA.

Code:
Set initialsh = Sheets("Sheet1")
Set transposesh = Sheets("Sheet2")
lr = initialsh.Cells(Rows.Count, 1).End(xlUp).Row
lc = initialsh.Cells(1, Columns.Count).End(xlToLeft).Column
If lr < 2 Or lc < 3 Then Exit Sub
With transposesh
    .Cells.ClearContents
    .Range("A1") = "Comps"
    .Range("B1") = "Amt."
    .Range("C1") = "Emp.ID"
End With
firstrow = 2
For i = 2 To lr
    Range(initialsh.Cells(1, 3), initialsh.Cells(1, lc)).Copy
    transposesh.Cells(firstrow, 1).PasteSpecial Paste:=xlPasteValues, Transpose:=True
    Range(initialsh.Cells(i, 3), initialsh.Cells(i, lc)).Copy
    transposesh.Cells(firstrow, 2).PasteSpecial Paste:=xlPasteValues, Transpose:=True
    Range(transposesh.Cells(firstrow, 3), transposesh.Cells(firstrow + lc - 3, 3)) = initialsh.Cells(i, 1)
    firstrow = firstrow + lc - 2
Next

Assumes your first table starts in A1. Sheet names will need changing to suit.
 
Upvote 0
VinodKrishnappa,

Here is another macro solution for you to consider that uses two arrays in memory that will adjust to the number of raw data rows, and, columns, and, should be much faster then copying and pasting.

You can change the raw data worksheet name in the macro.

The macro will create a new worksheet Results.

Sample raw data:


Excel 2007
ABCDEFGHIJ
1Emp. No.NameComp1Comp2Comp3Comp4Comp5Comp8Comp9
22002A2000800160025001250001500
32003B2200880160025001250001500
42004C240096016002500120002001500
5
Sheet1


And, after the macro in a new worksheet Results:


Excel 2007
ABC
1CompsAmt.Emp. ID
2Comp120002002
3Comp28002002
4Comp316002002
5Comp425002002
6Comp5125002002
7Comp915002002
8Comp122002003
9Comp28802003
10Comp316002003
11Comp425002003
12Comp5125002003
13Comp915002003
14Comp124002004
15Comp29602004
16Comp316002004
17Comp425002004
18Comp5120002004
19Comp82002004
20Comp915002004
21
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
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 TransposeData()
' hiker95, 09/16/2016, ME965091
Dim w1 As Worksheet, wr As Worksheet
Dim a As Variant, i As Long, c As Long
Dim o As Variant, j As Long
Application.ScreenUpdating = False
Set w1 = Sheets("Sheet1")   '<-- you can change the sheet name here
a = w1.Cells(1, 1).CurrentRegion
ReDim o(1 To (UBound(a, 2) - 2) * (UBound(a, 1) - 1), 1 To 3)
For i = 2 To UBound(a, 1)
  For c = 3 To UBound(a, 2)
    If Not a(i, c) = 0 Then
      j = j + 1
      o(j, 1) = a(1, c)
      o(j, 2) = a(i, c)
      o(j, 3) = a(i, 1)
    End If
  Next c
Next i
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"
Set wr = Sheets("Results")
With wr
  .UsedRange.Clear
  .Cells(1, 1).Resize(, 3).Value = Array("Comps", "Amt.", "Emp. ID")
  .Cells(2, 1).Resize(UBound(o, 1), UBound(o, 2)) = o
  .Columns(1).Resize(, UBound(o, 2)).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 TransposeData macro.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,688
Members
449,117
Latest member
Aaagu

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