Excel macro - change column data to multiple rows

jean299

New Member
Joined
Feb 27, 2007
Messages
26
I have a spreadsheet with 14 columns. Twelve of the columns contain categories of data that correspond to the entries in the other two columns. I need to change the list so that each item in each category occupies its own row, as follows:

Eg.

From this:

col1-----col2-----category1-----category2-----category3-----etc...
aaa------bbb-------------------------$55
ccc------ddd------$44---------------------------------$66


To this:

col1------col2------category------amount
aaa-------bbb------2-----------------$55
ccc-------ddd------1-----------------$44
ccc-------ddd------3-----------------$66


I know how to do this manually, but it needs to be done frequently on many different sheets. So, I need a way to do it automatically.

Many thanks for your help
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
jean299,

You will generally get much more help (and faster) in this forum if you can post your small samples (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
 
Upvote 0
Here is a sample. Note I had to cut off the 12th column as the HTMLMaker would only allow 13 columns and I have 14. The 12th column is just like the eleven before it.
Excel Workbook
ABCDEFGHIJKLM
1From this
2Funditem#1234567891011
30925406670-399,398
40929406670-25
50911408346-139,303-117,552-258,355-19,109-65,022-148,315-23,986-55,920-211,446-5,972-78,800
60925408346-227-182-45-5,718-327-482-1,645-45
70927408346-214-455-6,099-632455-8,886-38,268
80928408346-300-17,660-1001,639-9,832-300-80,167
90929408346-4,980-17,548-10,923-1,711-6,529-19,000-2,171-6,818-63,901-2,975-50,146
102350408346-482-3,364-2,255-218-1,259-3,000-218-10,678
110911425007-27,841-40,605-113,011-595-1,354-1,109-64-265-5,799-8,97753,486
120925425007-11,147-45-11,723
130927425007-2,742-28,74515,136
14
15To this
16Funditem#categoryAMOUNT
1713704066701-20.54
1813704066702-140.57
1913704066704-5,879.09
2013704066705-45.89
2113704066706-56.49
2213704066709-3.53
237424066709-4,872.33
2413704066707-40.6
257424066708-1,005.80
267424066707-3,636.34
2713704066708-52.96
2874240667012-3,432.63
29137040667011-3.53
30137040667010-13.8
Sheet1
Excel 2007
 
Upvote 0
I got this macro from TechGuy forums and it seems to work quite well:

Sub test()
LastRow = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LastRow

For Each Cell In Range("C" & i & ":N" & i)
If Cell <> "" Then
DestRow = WorksheetFunction.CountA(Columns(16)) + 1
Cells(DestRow, 16).Resize(, 2).Value = Cells(i, 1).Resize(, 2).Value
Cells(DestRow, 18) = Cell.Column - 2
Cells(DestRow, 19) = Cell.Value
End If
Next Cell

Next i
End Sub
 
Upvote 0
jean299,

Can you give us a screenshot of just column N?

Is Fund actually in cell A1 or A2?

Your To this does not correspond to the raw data in the From this.

A screenshot of just the raw data would help (without the From this).

And, another screenshot of just the results (from the raw data) would help.
 
Upvote 0
The example data above is just a demo of how the data is set out. The numbers don't actually match in the 'From this' and 'To this' sections. If I could get the full set of columns into the HTML Maker, I could show you from columns A to S how the macro works. It is brilliant. Here is a bit more information if you want to do this yourself or analyse how the macro works.

The macro maps the data from column A:N for the source data and from column P:S for the resulting data.

It is written specifically for two columns of fixed reference data and 12 columns of data that will be transposed to two columns.

You need to setup your data correctly before running the macro.

1. Paste the data on a sheet with only the 14 required columns (columns A:N). That is 2 columns of fixed data and 12 columns of data to be transposed.

2. Delete the column headers so data starts on row 1 column A.

3. Save as a macro enabled workbook.

4. Open the sheet and run the macro

5. Voila! The result will appear in 4 columns, P:S, starting on row 1.
 
Last edited:
Upvote 0
jean299,


Sample raw data on worksheet Sheet1 before the macro:


Excel Workbook
ABCDEFGHIJKLMN
1Funditem#123456789101112
2925406670-399,398
3929406670-25-25
4911408346-139,303-117,552-258,355-19,109-65,022-148,315-23,986-55,920-211,446-5,972-78,800-65,022
5925408346-227-182-45-5,718-327-482-1,645-45-45
6927408346-214-455-6,099-632455-8,886-38,268-6,099
7928408346-300-17,660-1001,639-9,832-300-80,167
8929408346-4,980-17,548-10,923-1,711-6,529-19,000-2,171-6,818-63,901-2,975-50,146-6,529
92350408346-482-3,364-2,255-218-1,259-3,000-218-10,678-1,259
10911425007-27,841-40,605-113,011-595-1,354-1,109-64-265-5,799-8,97753,486-1,354
11925425007-11,147-45-11,723
12927425007-2,742-28,74515,136-2,742
13
Sheet1





After the macro on a new worksheet Results (just a small sample for brevity):


Excel Workbook
ABCD
1FundItem#CategoryAmount
29254066701
39254066702
49254066703
59254066704
69254066705
79254066706-399398
89254066707
99254066708
109254066709
1192540667010
1292540667011
1392540667012
149294066701
159294066702
169294066703
179294066704
189294066705-25
199294066706
209294066707
219294066708
229294066709
2392940667010
2492940667011
2592940667012-25
269114083461-139303
279114083462-117552
289114083463-258355
299114083464-19109
309114083465-65022
319114083466-148315
329114083467-23986
339114083468-55920
349114083469-211446
3591140834610-5972
3691140834611-78800
3791140834612-65022
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, 04/19/2011
' http://www.mrexcel.com/forum/showthread.php?t=544487
Dim w1 As Worksheet, wR As Worksheet
Dim LR As Long, a 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
wR.Range("A1:D1") = [{"Fund","Item#","Category","Amount"}]
LR = w1.Cells(Rows.Count, 1).End(xlUp).Row
For a = 2 To LR Step 1
  NR = wR.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
  wR.Range("A" & NR).Resize(12, 2).Value = w1.Range("A" & a).Resize(, 2).Value
  wR.Range("C" & NR).Resize(12).Value = Application.Transpose(w1.Range("C1:N1").Value)
  wR.Range("D" & NR).Resize(12).Value = Application.Transpose(w1.Range("C" & a & ":N" & a).Value)
Next a
wR.UsedRange.Columns.AutoFit
wR.Activate
Application.ScreenUpdating = True
End Sub


Before you run the macro, save your workbook, Save As, a macro enabled workbook.


Then run the ReorgData macro.
 
Upvote 0
jean299,

Be back in a little while with the code for 5. Voila! The result will appear in 4 columns, P:S, starting on row 1.
 
Upvote 0
Aha! I see! So this is another macro that does the same thing, I guess. But it looks as though you don't have to remove the column headings. Is that right? And you have the results appear on a new sheet, which is good. It really doesn't matter where the result appears and probably a new sheet is best.
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,681
Members
452,937
Latest member
Bhg1984

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