Puzzler

tljenkin

Board Regular
Joined
Jun 14, 2007
Messages
147
I have a record thus:


Name Tag Week 1 Week 2

Peter hsdk £5 £10

How do I transpose this automatically so that I get the following results:

Week Name Tag Amount

Week 1 Peter hsdk £5
Week 2 Peter hsdk £10

Thanks
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi,

Try using a Pivot table to do this:

In Excel 2010:

  • Drag Week 1 into the 'Values' area
  • Drag Week 2 into the 'Values' area
  • Drag Values from the 'Column Labels' area into the 'Row Labels' area
  • Drag Name into the 'Row Labels' area (below the 'Values')
  • Drag Tag into the 'Row Labels' area
  • Click on the 'Design' tab at the top, Report Layout > Show in Tabular Form
  • Click on the 'Design' tab at the top, Subtotals > Do not show subtotals
  • Click on the 'Design' tab at the top, Grand Totals > Off for Rows and Columns
  • Change/format the headings on the table however you want them

In earlier Excel versions it will be similar - you just need to drag the stuff into the right places and turn off subtotals etc.
 
Last edited:
Upvote 0
Thanks but would your answer be the same if I had to do this with 300 weeks or columns which is exactly what I have to do...
 
Upvote 0
Hi,

Perhaps run this macro instead:

Code:
Option Explicit
Option Base 1

Sub transpose()

Dim i As Long
Dim j As Long
Dim k As Long
Dim rData As Variant

With Sheet2
    ' add column headings
    .Cells(1, "A") = "Week"
    .Cells(1, "B") = "Name"
    .Cells(1, "C") = "Tag"
    .Cells(1, "D") = "Amount"
    
    ' add data
    rData = Sheet1.Range("[B]A1:L5[/B]") 'change this to your actual data range (including the headings)
    For i = 2 To UBound(rData, 1)
        For j = 3 To UBound(rData, 2)
            .Cells(j + k - 1, "A") = rData(1, j)
            .Cells(j + k - 1, "B") = rData(i, 1)
            .Cells(j + k - 1, "C") = rData(i, 2)
            .Cells(j + k - 1, "D") = rData(i, j)
        Next j
        k = k + UBound(rData, 2) - 2
    Next i
End With
    
End Sub

This will put the data in Sheet2 in the format you want. I'm not sure of your reasons for wanting the data in this structure though, because your repeating a lot of data so there might be better ways of organising this?
 
Upvote 0
It works but it brings through the word amount in the week field, how do I stop this happening?

Never used ubound before so not sure what to adjust

Thanks
 
Upvote 0
The word 'Amount' should only show up once in cell D1 as per your original post - I'm not clear how else you want it?

UBound returns the upper bound of an array. For example, in this case UBound(rData,1) would be the number of rows your data contains, and Ubound(rData,2) would be the number of columns.
 
Upvote 0
tljenkin,


Sample raw data in worksheet Sheet1:


Excel Workbook
ABCDEFGHIJKL
1NameTagWeek 1Week 2Week 3Week 4Week 5Week 6Week 7Week 8Week 9Week 10
2Peterhsdk$5$10$6$12$7$14$8$16$9$18
3tljenkinPuzzler$6$12$7$14$8$16$9$18$10$20
4
Sheet1





After the macro in a new worksheet Results:[/n]


Excel Workbook
ABCD
1WeekNameTagAmont
2Week 1Peterhsdk$5
3Week 2Peterhsdk$10
4Week 3Peterhsdk$6
5Week 4Peterhsdk$12
6Week 5Peterhsdk$7
7Week 6Peterhsdk$14
8Week 7Peterhsdk$8
9Week 8Peterhsdk$16
10Week 9Peterhsdk$9
11Week 10Peterhsdk$18
12Week 1tljenkinPuzzler$6
13Week 2tljenkinPuzzler$12
14Week 3tljenkinPuzzler$7
15Week 4tljenkinPuzzler$14
16Week 5tljenkinPuzzler$8
17Week 6tljenkinPuzzler$16
18Week 7tljenkinPuzzler$9
19Week 8tljenkinPuzzler$18
20Week 9tljenkinPuzzler$10
21Week 10tljenkinPuzzler$20
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
Option Base 1
Sub ReorgData()
' hiker95, 09/25/2011
' http://www.mrexcel.com/forum/showthread.php?t=581001
Dim w1 As Worksheet, wR As Worksheet
Dim I(), O()
Dim LR As Long, LC As Long, r As Long, c As Long, n As Long
Set w1 = Worksheets("Sheet1")
LR = w1.Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
LC = w1.Cells.Find("*", , xlValues, xlWhole, xlByColumns, xlPrevious, False).Column
I = w1.Range("A1").CurrentRegion.Resize(, LC).Value
ReDim O(1 To (LC - 2) * (LR - 1) + 1, 1 To 4)
O(1, 1) = "Week"
O(1, 2) = "Name"
O(1, 3) = "Tag"
O(1, 4) = "Amont"
n = 1
For r = 2 To UBound(I) Step 1
  For c = 3 To LC Step 1
    n = n + 1
    O(n, 1) = I(1, c)
    O(n, 2) = I(r, 1)
    O(n, 3) = I(r, 2)
    O(n, 4) = I(r, c)
  Next c
Next r
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"
Set wR = Worksheets("Results")
wR.UsedRange.Clear
wR.Range("A1").Resize(UBound(O), 4).Value = O
wR.Range("D2").Resize(UBound(O)).NumberFormat = "$#,##0_);($#,##0)"
wR.UsedRange.Columns.AutoFit
wR.Activate
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
tljenkin,


After testing the macro, you may want to remove the next line of code:

Code:
wR.Range("D2").Resize(UBound(O)).NumberFormat = "$#,##0_);($#,##0)"
 
Upvote 0
circled chicken and hiker 95, you are missing the field "amount" from the original data or lets just say you are missing one field before the week fields.

So I guess my question is, if there were additional columns/ fields before the week fields, how can I tweak the code each time to reflect this so that it doesnt pull the field name into the week field?

Thanks
 
Upvote 0
tljenkin,

The only way we are going to get it right is if we can see your raw data.


You will generally get much more help (and faster) in this forum if you can post your small samples (sensitive data scrubbed/removed/changed) (what you have and what you expect to achieve) directly in the forum.

To attach screenshots, see below in my Signature block: Post a screen shot with one of these:

If you are not able to give us screenshots, see below in my Signature block: You can upload your workbook to Box Net, mark the workbook for sharing, and provide us with a link to your workbook.
 
Upvote 0

Forum statistics

Threads
1,224,548
Messages
6,179,451
Members
452,915
Latest member
hannnahheileen

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