Transforming a weird data layout structure

jpchen0321

New Member
Joined
Feb 14, 2011
Messages
2
Hi,

Using VBA...

I have sets of data that is represented in this form (comma represents cell separation):

1, data1a, data1b, data1c
2, data2a, data2b
3, data3a, data3b, data3c, data3d

How can I transform that data into the following form:

1, data1a
1, data1b
1, data1c
2, data2a
2, data2b
3, data3a
3, data3b
3, data3c
3, data3d

Thanks.
 
Last edited:

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
jpchen0321,

Welcome to the MrExcel forum.


Sample data in worksheet Sheet1:


Excel Workbook
ABCDE
11data1adata1bdata1c
22data2adata2b
33data3adata3bdata3cdata3d
4
Sheet1





After the macro in a new worksheet Results:


Excel Workbook
AB
11data1a
21data1b
31data1c
42data2a
52data2b
63data3a
73data3b
83data3c
93data3d
10
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, 02/14/2011
' http://www.mrexcel.com/forum/showthread.php?t=529103
Dim w1 As Worksheet, wR As Worksheet
Dim LR As Long, a As Long, LC As Long, n As Long, NR 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
LR = w1.Cells(Rows.Count, 1).End(xlUp).Row
NR = 1
For a = 1 To LR Step 1
  LC = w1.Cells(a, Columns.Count).End(xlToLeft).Column
  n = LC - 1
  wR.Range("A" & NR).Resize(n) = w1.Range("A" & a)
  wR.Range("B" & NR).Resize(n) = Application.Transpose(w1.Range("B" & a).Resize(, n))
  NR = wR.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
Next a
wR.UsedRange.Columns.AutoFit
wR.Activate
Application.ScreenUpdating = True
End Sub


Then run the ReorgData macro.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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