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
Excel Workbook | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | K | L | |||
1 | Name | Tag | Week 1 | Week 2 | Week 3 | Week 4 | Week 5 | Week 6 | Week 7 | Week 8 | Week 9 | Week 10 | ||
2 | Peter | hsdk | $5 | $10 | $6 | $12 | $7 | $14 | $8 | $16 | $9 | $18 | ||
3 | tljenkin | Puzzler | $6 | $12 | $7 | $14 | $8 | $16 | $9 | $18 | $10 | $20 | ||
4 | ||||||||||||||
Sheet1 |
Excel Workbook | ||||||
---|---|---|---|---|---|---|
A | B | C | D | |||
1 | Week | Name | Tag | Amont | ||
2 | Week 1 | Peter | hsdk | $5 | ||
3 | Week 2 | Peter | hsdk | $10 | ||
4 | Week 3 | Peter | hsdk | $6 | ||
5 | Week 4 | Peter | hsdk | $12 | ||
6 | Week 5 | Peter | hsdk | $7 | ||
7 | Week 6 | Peter | hsdk | $14 | ||
8 | Week 7 | Peter | hsdk | $8 | ||
9 | Week 8 | Peter | hsdk | $16 | ||
10 | Week 9 | Peter | hsdk | $9 | ||
11 | Week 10 | Peter | hsdk | $18 | ||
12 | Week 1 | tljenkin | Puzzler | $6 | ||
13 | Week 2 | tljenkin | Puzzler | $12 | ||
14 | Week 3 | tljenkin | Puzzler | $7 | ||
15 | Week 4 | tljenkin | Puzzler | $14 | ||
16 | Week 5 | tljenkin | Puzzler | $8 | ||
17 | Week 6 | tljenkin | Puzzler | $16 | ||
18 | Week 7 | tljenkin | Puzzler | $9 | ||
19 | Week 8 | tljenkin | Puzzler | $18 | ||
20 | Week 9 | tljenkin | Puzzler | $10 | ||
21 | Week 10 | tljenkin | Puzzler | $20 | ||
Results |
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