transpose data from one sheet to another

maxzoran

Board Regular
Joined
Feb 1, 2008
Messages
63
Good morning,

I submitted a thread for this previously but didn't have the proper elements in it to get help. I have a data set that looks similar to the RAW tab in the following file http://www.box.net/shared/fpgqyv9ok3nghx9foqlq

What I need is a formula or a macro to put the data in the form of the TRANSPOSED tab where the "data #" is taken from the headers in columns A and C (1st, 2nd, 3rd...n) then the nth and *nth data is grouped with the "data #." As you can see from the example, the data is not consistent with the number of data sets per group so my trouble lies with ignoring the blanks. I wrote a small nested if statement but quickly realized that it would be a huge formula as my actual data set is much larger than this example. So, I am looking for some help from the infinite wisdom of this board. Any formula tips or macros are sure appreciated.

fyi...I am using Excel 2010

Thanks in advance!
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
maxzoran,


Thanks for the workbook.


Sample raw data in worksheet RAW (with the data grouped in 10 rows, and a space row between groups):


Excel Workbook
ABCD
11st data*1st data2nd data*2nd data
21A5E
32B6F
43C7G
54D8H
69I
710J
8
9
10
11
123rd data*3rd data4th data*4th data
1311K14N
1412L15O
1513M16P
1617Q
1718R
1819S
1920T
2021U
2122V
22
235th data*5th data6th data*6th data
2423W24X
2525Y
2626Z
2727AA
2828BB
29
30
31
32
33
347th data*7th data8th data*8th data
3529CC32FF
3630DD33GG
3731EE34HH
3835II
3936JJ
4037KK
4138LL
42
43
44
459th data*9th data10th data*10th data
4639MM41OO
4740NN42PP
4843QQ
4944RR
5045SS
5146TT
5247UU
5348VV
5449WW
RAW





After the macro in a new worksheet Results:


Excel Workbook
ABC
1data #Data*data
211A
312B
413C
514D
625E
726F
827G
928H
1029I
11210J
12311K
13312L
14313M
15414N
16415O
17416P
18417Q
19418R
20419S
21420T
22421U
23422V
24523W
25624X
26625Y
27626Z
28627AA
29628BB
30729CC
31730DD
32731EE
33832FF
34833GG
35834HH
36835II
37836JJ
38837KK
39838LL
40939MM
41940NN
421041OO
431042PP
441043QQ
451044RR
461045SS
471046TT
481047UU
491048VV
501049WW
51
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, 10/09/2011
' http://www.mrexcel.com/forum/showthread.php?t=584276
Dim w1 As Worksheet, wR As Worksheet
Dim LR As Long, r As Long, rr As Long, c As Long, NR As Long
Dim Sp, MyA As String
Application.ScreenUpdating = False
Set w1 = Worksheets("RAW")
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"
Set wR = Worksheets("Results")
wR.UsedRange.Clear
wR.Range("A1:C1") = [{"data #","Data","*data"}]
LR = w1.Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
For r = 1 To LR Step 11
  For c = 1 To 4 Step 2
    Sp = Split(w1.Cells(r, c), " ")
    MyA = Left(Sp(0), Len(Sp(0)) - 2)
    For rr = r + 1 To r + 9 Step 1
      If w1.Cells(rr, c) <> "" Then
        NR = wR.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
        wR.Range("A" & NR) = MyA
        wR.Range("B" & NR).Resize(, 2).Value = w1.Cells(rr, c).Resize(, 2).Value
      End If
    Next rr
  Next c
Next r
wR.Activate
With wR.UsedRange
  .HorizontalAlignment = xlCenter
  .Columns.AutoFit
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


Then run the ReorgData macro.
 
Upvote 0
Here is another macro for you to consider...

Code:
Sub TransposeDataTables()
  Dim LastRow As Long
  Application.ScreenUpdating = False
  With Worksheets("TRANSPOSED")
    .Range("A1").Value = "Data #"
    .Range("B1").Value = "Data"
    .Range("C1").Value = "*Data"
    Worksheets("RAW").Columns("A:B").SpecialCells(xlConstants).Copy .Range("B2")
    LastRow = .Cells(Rows.Count, "B").End(xlUp).Row
    Worksheets("RAW").Columns("C:D").SpecialCells(xlConstants).Copy .Cells(LastRow + 1, "B")
    .Columns("B:C").ClearFormats
    LastRow = .Cells(Rows.Count, "B").End(xlUp).Row
    With .Range("A2:A" & LastRow)
      .FormulaR1C1 = "=IF(COUNTIF(RC2,""*data""),LEFT(RC2,LEN(RC2)-7),"""")"
      .Value = .Value
      .SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
      .Value = .Value
    End With
    .Columns("A:C").Sort Range("A1"), xlAscending, Range("B1"), , xlAscending, Range("C1"), xlAscending, xlYes, True
    With .Range("B2:B" & LastRow)
      .Replace "*data", "", xlPart
      .SpecialCells(xlBlanks).EntireRow.Delete
    End With
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
maxzoran,

You are very welcome.

Glad I could help.

Thanks for the feedback.

Come back anytime.
 
Upvote 0

Forum statistics

Threads
1,216,115
Messages
6,128,915
Members
449,478
Latest member
Davenil

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