Macro to rearrange structure of report.

Carl_H

New Member
Joined
Sep 16, 2014
Messages
18
Hello,

Is it possible to change the structure of this report:

2nia59i.jpg



To look like this:
2924avk.jpg
[/IMG]

if anyone can help me I would be very grateful.

My report has a lot more rows than above but the columns are the same.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hello,

I was thinking maybe it will be easier if a make a table that you can copy paste into an excelsheet. If you need further information I'll do my best to provide it for you.

Also there is 2 blanks between * and the cost center name, and 3 blanks before the cost element if that is important facts.
[TABLE="width: 679"]
<TBODY>[TR]
[TD]Act. per 8</SPAN>
[/TD]
[TD]Plan. per 8</SPAN>
[/TD]
[TD]Var. per 8</SPAN>
[/TD]
[TD]Cost centers/Cost elements</SPAN>
[/TD]
[TD]Act per 01 - 8</SPAN>
[/TD]
[TD]Plan version 3</SPAN>
[/TD]
[/TR]
[TR]
[TD="align: right"]1769</SPAN>
[/TD]
[TD="align: right"]41666,66</SPAN>
[/TD]
[TD="align: right"]-39897,66</SPAN>
[/TD]
[TD]591202 Customer magazines, production</SPAN>
[/TD]
[TD="align: right"]453677</SPAN>
[/TD]
[TD="align: right"]500000</SPAN>
[/TD]
[/TR]
[TR]
[TD="align: right"]-140</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]-140</SPAN>
[/TD]
[TD]591816 Customer magazines</SPAN>
[/TD]
[TD="align: right"]-2520</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[/TR]
[TR]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[TD]625001 Postal expenses</SPAN>
[/TD]
[TD="align: right"]-1499990,5</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[/TR]
[TR]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[TD]655901 Other consultant fees</SPAN>
[/TD]
[TD="align: right"]217271</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[/TR]
[TR]
[TD="align: right"]148</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]148</SPAN>
[/TD]
[TD]659202 Hired Services Elanders (Publ Store)</SPAN>
[/TD]
[TD="align: right"]752</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[/TR]
[TR]
[TD="align: right"]1777</SPAN>
[/TD]
[TD="align: right"]41666,66</SPAN>
[/TD]
[TD="align: right"]-39889,66</SPAN>
[/TD]
[TD]* 19065 Customer magazines</SPAN>
[/TD]
[TD="align: right"]-830810,5</SPAN>
[/TD]
[TD="align: right"]500000</SPAN>
[/TD]
[/TR]
[TR]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[TD]564901 Demo/test vehicle miscellaneous</SPAN>
[/TD]
[TD="align: right"]107138</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[/TR]
[TR]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[TD]* 19066 VT Press Test</SPAN>
[/TD]
[TD="align: right"]107138</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[/TR]
[TR]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]4166,66</SPAN>
[/TD]
[TD="align: right"]-4166,66</SPAN>
[/TD]
[TD]591101 Daily press media space</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]50000</SPAN>
[/TD]
[/TR]
[TR]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]41666,66</SPAN>
[/TD]
[TD="align: right"]-41666,66</SPAN>
[/TD]
[TD]591201 Magazine media space</SPAN>
[/TD]
[TD="align: right"]329504,25</SPAN>
[/TD]
[TD="align: right"]500000</SPAN>
[/TD]
[/TR]
[TR]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[TD]591802 Advertising</SPAN>
[/TD]
[TD="align: right"]10000</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[/TR]
[TR]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[TD]595002 POS production</SPAN>
[/TD]
[TD="align: right"]13158</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[/TR]
[TR]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[TD]597102 Photos production</SPAN>
[/TD]
[TD="align: right"]1500</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[/TR]
[TR]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]4583,34</SPAN>
[/TD]
[TD="align: right"]-4583,34</SPAN>
[/TD]
[TD]597401 Internet</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]55000</SPAN>
[/TD]
[/TR]
[TR]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]8333,34</SPAN>
[/TD]
[TD="align: right"]-8333,34</SPAN>
[/TD]
[TD]599701 Other advertising costs</SPAN>
[/TD]
[TD="align: right"]136700</SPAN>
[/TD]
[TD="align: right"]100000</SPAN>
[/TD]
[/TR]
[TR]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]8333,34</SPAN>
[/TD]
[TD="align: right"]-8333,34</SPAN>
[/TD]
[TD]615001 Printed matter</SPAN>
[/TD]
[TD="align: right"]11406</SPAN>
[/TD]
[TD="align: right"]100000</SPAN>
[/TD]
[/TR]
[TR]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]67083,34</SPAN>
[/TD]
[TD="align: right"]-67083,34</SPAN>
[/TD]
[TD]* 19067 VT Advertising</SPAN>
[/TD]
[TD="align: right"]502268,25</SPAN>
[/TD]
[TD="align: right"]805000</SPAN>
[/TD]
[/TR]
[TR]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]2083,34</SPAN>
[/TD]
[TD="align: right"]-2083,34</SPAN>
[/TD]
[TD]564901 Demo/test vehicle miscellaneous</SPAN>
[/TD]
[TD="align: right"]25120</SPAN>
[/TD]
[TD="align: right"]25000</SPAN>
[/TD]
[/TR]
[TR]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[TD]581003 Tickets other countries</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[/TR]
[TR]
[TD="align: right"]14290</SPAN>
[/TD]
[TD="align: right"]1250</SPAN>
[/TD]
[TD="align: right"]13040</SPAN>
[/TD]
[TD]597102 Photos production</SPAN>
[/TD]
[TD="align: right"]14290</SPAN>
[/TD]
[TD="align: right"]15000</SPAN>
[/TD]
[/TR]
[TR]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]28750</SPAN>
[/TD]
[TD="align: right"]-28750</SPAN>
[/TD]
[TD]598401 Sponsorship</SPAN>
[/TD]
[TD="align: right"]326058,2</SPAN>
[/TD]
[TD="align: right"]345000</SPAN>
[/TD]
[/TR]
[TR]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[TD]609801 Other sales costs</SPAN>
[/TD]
[TD="align: right"]173,41</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[/TR]
[TR]
[TD="align: right"]25119,13</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[TD="align: right"]25119,13</SPAN>
[/TD]
[TD]783501 Depreciation of cars</SPAN>
[/TD]
[TD="align: right"]25119,13</SPAN>
[/TD]
[TD="align: right"]0</SPAN>
[/TD]
[/TR]
[TR]
[TD="align: right"]39409,13</SPAN>
[/TD]
[TD="align: right"]32083,34</SPAN>
[/TD]
[TD="align: right"]7325,79</SPAN>
[/TD]
[TD]* 19069 VT PR/Media</SPAN>
[/TD]
[TD="align: right"]390760,74</SPAN>
[/TD]
[TD="align: right"]385000</SPAN>
[/TD]
[/TR]
</TBODY>[/TABLE]
 
Last edited:
Upvote 0
With the additional headings removed try:

Code:
Sub Test()
    Dim ShNew As Worksheet
    Dim LastRow As Long
    Dim First As Long
    Dim r As Long
    Dim i As Long
    Set ShNew = Worksheets.Add
    ShNew.Range("A1:I1").Value = Array("Act. per 8", "Plan. per 8", "Var. per 8", "Cost center", "Cost center text", "Cost element", "Cost element text", "Act per 01 - 8", "Plan version 3")
    First = 2
    r = 2
    With Worksheets("Sheet1")
        LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
        For i = 2 To LastRow
            If Left(.Range("D" & i).Value, 1) <> "*" Then
                .Range("A" & i).Resize(, 3).Copy ShNew.Range("A" & r)
                ShNew.Range("F" & r).Value = Left(.Range("D" & i).Value, 6)
                ShNew.Range("G" & r).Value = Mid(.Range("D" & i).Value, 8)
                .Range("E" & i).Resize(, 2).Copy ShNew.Range("H" & r)
                r = r + 1
            Else
                ShNew.Range("D" & First & ":D" & r - 1).Value = Mid(Trim(.Range("D" & i).Value), 3, 5)
                ShNew.Range("E" & First & ":E" & r - 1).Value = Mid(Trim(.Range("D" & i).Value), 9)
                First = r
            End If
        Next i
    End With
End Sub
 
Upvote 0
Hello,

First of all, thanks for your effort so far. It almost worked.

The result when I ran the macro can be found below: As you can see, last digit of cost center is missing, and if you paste the data below into an excel sheet you will also see that the cost element number only displays 3 digits in the correct column.

If you reply, I will not have time to check new result until tomorrow as I am about to leave work.

And by the way: I just started my new job and when I can start to use this macro I will be able to rebuild our report which will easily save me 30-40 hours a month, not having to do any copy paste which my former colleague seemed to like :) So once again. Thanks a lot!

EDIT: I did not include cost center 19066 below.
[TABLE="width: 901"]
<TBODY>[TR]
[TD]Act. per 8</SPAN>
[/TD]
[TD]Plan. per 8</SPAN>
[/TD]
[TD]Var. per 8</SPAN>
[/TD]
[TD]Cost center</SPAN>
[/TD]
[TD]Cost center text</SPAN>
[/TD]
[TD]Cost element</SPAN>
[/TD]
[TD]Cost element text</SPAN>
[/TD]
[TD]Act per 01 - 8</SPAN>
[/TD]
[TD]Plan version 3</SPAN>
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD="align: right"]4 166,66</SPAN>
[/TD]
[TD]4 166,66-</SPAN>
[/TD]
[TD="align: right"]1906</SPAN>
[/TD]
[TD]VT Advertising</SPAN>
[/TD]
[TD="align: right"]591</SPAN>
[/TD]
[TD]01 Daily press media space</SPAN>
[/TD]
[TD][/TD]
[TD="align: right"]50 000,00</SPAN>
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD="align: right"]41 666,66</SPAN>
[/TD]
[TD]41 666,66-</SPAN>
[/TD]
[TD="align: right"]1906</SPAN>
[/TD]
[TD]VT Advertising</SPAN>
[/TD]
[TD="align: right"]591</SPAN>
[/TD]
[TD]01 Magazine media space</SPAN>
[/TD]
[TD="align: right"]329 504,25</SPAN>
[/TD]
[TD="align: right"]500 000,00</SPAN>
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="align: right"]1906</SPAN>
[/TD]
[TD]VT Advertising</SPAN>
[/TD]
[TD="align: right"]591</SPAN>
[/TD]
[TD]02 Advertising</SPAN>
[/TD]
[TD="align: right"]10 000,00</SPAN>
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="align: right"]1906</SPAN>
[/TD]
[TD]VT Advertising</SPAN>
[/TD]
[TD="align: right"]595</SPAN>
[/TD]
[TD]02 POS production</SPAN>
[/TD]
[TD="align: right"]13 158,00</SPAN>
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="align: right"]1906</SPAN>
[/TD]
[TD]VT Advertising</SPAN>
[/TD]
[TD="align: right"]597</SPAN>
[/TD]
[TD]02 Photos production</SPAN>
[/TD]
[TD="align: right"]1 500,00</SPAN>
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD="align: right"]4 583,34</SPAN>
[/TD]
[TD]4 583,34-</SPAN>
[/TD]
[TD="align: right"]1906</SPAN>
[/TD]
[TD]VT Advertising</SPAN>
[/TD]
[TD="align: right"]597</SPAN>
[/TD]
[TD]01 Internet</SPAN>
[/TD]
[TD][/TD]
[TD="align: right"]55 000,00</SPAN>
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD="align: right"]8 333,34</SPAN>
[/TD]
[TD]8 333,34-</SPAN>
[/TD]
[TD="align: right"]1906</SPAN>
[/TD]
[TD]VT Advertising</SPAN>
[/TD]
[TD="align: right"]599</SPAN>
[/TD]
[TD]01 Other advertising costs</SPAN>
[/TD]
[TD="align: right"]136 700,00</SPAN>
[/TD]
[TD="align: right"]100 000,00</SPAN>
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD="align: right"]8 333,34</SPAN>
[/TD]
[TD]8 333,34-</SPAN>
[/TD]
[TD="align: right"]1906</SPAN>
[/TD]
[TD]VT Advertising</SPAN>
[/TD]
[TD="align: right"]615</SPAN>
[/TD]
[TD]01 Printed matter</SPAN>
[/TD]
[TD="align: right"]11 406,00</SPAN>
[/TD]
[TD="align: right"]100 000,00</SPAN>
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD="align: right"]2 083,34</SPAN>
[/TD]
[TD]2 083,34-</SPAN>
[/TD]
[TD="align: right"]1906</SPAN>
[/TD]
[TD]VT PR/Media</SPAN>
[/TD]
[TD="align: right"]564</SPAN>
[/TD]
[TD]01 Demo/test vehicle miscellaneous</SPAN>
[/TD]
[TD="align: right"]25 120,00</SPAN>
[/TD]
[TD="align: right"]25 000,00</SPAN>
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="align: right"]1906</SPAN>
[/TD]
[TD]VT PR/Media</SPAN>
[/TD]
[TD="align: right"]581</SPAN>
[/TD]
[TD]03 Tickets other countries</SPAN>
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]14 290,00</SPAN>
[/TD]
[TD="align: right"]1 250,00</SPAN>
[/TD]
[TD="align: right"]13 040,00</SPAN>
[/TD]
[TD="align: right"]1906</SPAN>
[/TD]
[TD]VT PR/Media</SPAN>
[/TD]
[TD="align: right"]597</SPAN>
[/TD]
[TD]02 Photos production</SPAN>
[/TD]
[TD="align: right"]14 290,00</SPAN>
[/TD]
[TD="align: right"]15 000,00</SPAN>
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD="align: right"]28 750,00</SPAN>
[/TD]
[TD]28 750,00-</SPAN>
[/TD]
[TD="align: right"]1906</SPAN>
[/TD]
[TD]VT PR/Media</SPAN>
[/TD]
[TD="align: right"]598</SPAN>
[/TD]
[TD]01 Sponsorship</SPAN>
[/TD]
[TD="align: right"]326 058,20</SPAN>
[/TD]
[TD="align: right"]345 000,00</SPAN>
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="align: right"]1906</SPAN>
[/TD]
[TD]VT PR/Media</SPAN>
[/TD]
[TD="align: right"]609</SPAN>
[/TD]
[TD]01 Other sales costs</SPAN>
[/TD]
[TD="align: right"]173,41</SPAN>
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]25 119,13</SPAN>
[/TD]
[TD][/TD]
[TD="align: right"]25 119,13</SPAN>
[/TD]
[TD="align: right"]1906</SPAN>
[/TD]
[TD]VT PR/Media</SPAN>
[/TD]
[TD="align: right"]783</SPAN>
[/TD]
[TD]01 Depreciation of cars</SPAN>
[/TD]
[TD="align: right"]25 119,13</SPAN>
[/TD]
[TD][/TD]
[/TR]
</TBODY>[/TABLE]
 
Last edited:
Upvote 0
This is what I got with your sample data:


Excel 2010
ABCDEFGHI
1Act. per 8Plan. per 8Var. per 8Cost centerCost center textCost elementCost element textAct per 01 - 8Plan version 3
2176941666.66-39897.6619065Customer magazines591202Customer magazines. production453677500000
3-1400-14019065Customer magazines591816Customer magazines-25200
400019065Customer magazines625001Postal expenses-1499990.50
500019065Customer magazines655901Other consultant fees2172710
6148014819065Customer magazines659202Hired Services Elanders (Publ Store)7520
700019066VT Press Test564901Demo/test vehicle miscellaneous1071380
804166.66-4166.6619067VT Advertising591101Daily press media space050000
9041666.66-41666.6619067VT Advertising591201Magazine media space329504.25500000
1000019067VT Advertising591802Advertising100000
1100019067VT Advertising595002POS production131580
1200019067VT Advertising597102Photos production15000
1304583.34-4583.3419067VT Advertising597401Internet055000
1408333.34-8333.3419067VT Advertising599701Other advertising costs136700100000
1508333.34-8333.3419067VT Advertising615001Printed matter11406100000
1602083.34-2083.3419069VT PR/Media564901Demo/test vehicle miscellaneous2512025000
1700019069VT PR/Media581003Tickets other countries00
181429012501304019069VT PR/Media597102Photos production1429015000
19028750-2875019069VT PR/Media598401Sponsorship326058.2345000
2000019069VT PR/Media609801Other sales costs173.410
2125119.13025119.1319069VT PR/Media783501Depreciation of cars25119.130
Sheet4
 
Upvote 0
Hello,

with the data I pasted at first it works beautifully. However, it seems extra "blanks" have been removed when I pasted the data. My data set looks like * "blank" "blank" 19065 Customer magazines (* 19065 Customer magazines) and for the cost elements there is e.g. "blank" "blank" "blank" 591202 Customer magazines, production ( 591202 Customer magazines, production).
 
Upvote 0
How about?

Code:
Sub Test()
    Dim ShNew As Worksheet
    Dim LastRow As Long
    Dim First As Long
    Dim r As Long
    Dim i As Long
    Set ShNew = Worksheets.Add
    ShNew.Range("A1:I1").Value = Array("Act. per 8", "Plan. per 8", "Var. per 8", "Cost center", "Cost center text", "Cost element", "Cost element text", "Act per 01 - 8", "Plan version 3")
    First = 2
    r = 2
    With Worksheets("Sheet1")
        LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
        For i = 2 To LastRow
            If Left(.Range("D" & i).Value, 1) <> "*" Then
                .Range("A" & i).Resize(, 3).Copy ShNew.Range("A" & r)
                ShNew.Range("F" & r).Value = Left(WorksheetFunction.Trim(.Range("D" & i).Value), 6)
                ShNew.Range("G" & r).Value = Mid(WorksheetFunction.Trim(.Range("D" & i).Value), 8)
                .Range("E" & i).Resize(, 2).Copy ShNew.Range("H" & r)
                r = r + 1
            Else
                ShNew.Range("D" & First & ":D" & r - 1).Value = Mid(WorksheetFunction.Trim(.Range("D" & i).Value), 3, 5)
                ShNew.Range("E" & First & ":E" & r - 1).Value = Mid(WorksheetFunction.Trim(.Range("D" & i).Value), 9)
                First = r
            End If
        Next i
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,416
Messages
6,178,513
Members
452,853
Latest member
philipnjk64

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