Macro to roll up common rows.

ssully87

New Member
Joined
Nov 26, 2014
Messages
8
Hello,

I'm trying to write a Macro but i have not mastered functions and what not so I am looking for a little help. I have the below information. My real report does not look like this, however to maintain my companies privacy I have only added what needs to be adjusted.
I would write simple macros, however the report is tens of thousands of lines and without functions it will take a long time to run the macro. Also i'm not sure how I would accomplish this with simple macros as the data is not always consistent. Sometimes one invoice could have one line, and sometimes one invoice could have up to 9 lines. In any given circumstance there will be no more than 3 Producers or PRD.

InvoiceProducerCommision%Amount
B12659prod1278.06202,602.10
B12659prod11000
B12659GIVEN000
B12660prod12169.532016,953.10
B12660prod11000
B12660GIVEN000
A147044prod3147.5256,941.00
A147044prod1000
C12850prod10196.031526,138.08
C12850prod9130.69100
C12855prod105.4815366.38
C12855prod93.65100
C12856prod1031.88152,125.47
C12856prod921.25100
C12865prod76610660
C12865prod899150
C12866prod74.4610446.57
C12866prod86.69150
C12869prod71.0110101.65
C12869prod81.52150
C12970HOUSE000
C12970prod7000
B12970HOUSE00715.28
B12970prod7000
B13014prod628.8220960.62
B13014GIVEN000
C13016prod618.220758.26
C13016GIVEN000
C13166prod378.77154,375.84
C13166prod5000
C13166prod1000
B13167prod3101.29155,627.18
B13167prod000
B13167prod1000
A13177prod223.14102,012.00
A13177prod3000

<tbody>
</tbody>


I am trying to roll up the rows so that every invoice only has on line item, but still shows all producers and amounts on the invoice. Then add a column to show the total amount of the invoice like the below.
InvoiceAmountPRD1Com1PRD 1 %PRD2COM2PRD 2 %PRD3COM3PRD 3 %Total Amount
B126592,602.10prod1278.0620prod1100GIVEN002,602.10
B126590prod1100
B126590GIVEN00
B1266016,953.10prod12169.5320prod1100GIVEN0016,953.10
B126600prod1100
B126600GIVEN00
A1470446,941.00prod3147.525prod1006,941.00
A1470440prod100
C1285026,138.08prod10196.0315prod9130.71026,138.08
C128500prod9130.6910
C12855366.38prod105.4815prod93.6510366.38
C128550prod93.6510
C128562,125.47prod1031.8815prod921.25102,125.47
C128560prod921.2510
C12865660prod76610prod89915660.00
C128650prod89915
C12866446.57prod74.4610prod86.6915446.57
C128660prod86.6915
C12869101.65prod71.0110prod81.5215101.65
C128690prod81.5215
C129700HOUSE00prod7000.00
C129700prod700
B12970715.28HOUSE00prod700715.28
B129700prod700
B13014960.62prod628.8220GIVEN00960.62
B130140GIVEN00
C13016758.26prod618.220GIVEN00758.26
C130160GIVEN00
C131664,375.84prod378.7715prod500prod1004,375.84
C131660prod500
C131660prod100
B131675,627.18prod3101.2915prod00prod1005,627.18
B131670prod00
B131670prod100
A131772,012.00prod223.1410prod3002,012.00
A131770prod300
1803-1,335.45BRB-267.0920-8,903.030GIVEN0000.00
18030GIVEN000
18031,335.45BRB267.09208,903.03
18030GIVEN000

<tbody>
</tbody>



Then it would remove all the duplicate invoice rows to only show the below.

InvoiceAmountPRD1Com1PRD 1 %PRD2COM2PRD 2 %PRD3COM3PRD 3 %Total Amount
B126592,602.10prod1278.0620prod1100GIVEN002602.1
B1266016,953.10prod12169.5320prod1100GIVEN0016953.1
A1470446,941.00prod3147.525prod1006941
C1285026,138.08prod10196.0315prod9130.71026138.08
C12855366.38prod105.4815prod93.6510366.38
C128562,125.47prod1031.8815prod921.25102125.47
C12865660prod76610prod89915660
C12866446.57prod74.4610prod86.6915446.57
C12869101.65prod71.0110prod81.5215101.65
C129700HOUSE00prod7000
B12970715.28HOUSE00prod700715.28
B13014960.62prod628.8220GIVEN00960.62
C13016758.26prod618.220GIVEN00758.26
C131664,375.84prod378.7715prod500prod1004375.84
B131675,627.18prod3101.2915prod00prod1005627.18
A131772,012.00prod223.1410prod3002012
1803-1,335.45BRB-267.0920-8,903.030GIVEN0000

<tbody>
</tbody>


Let me know if you need any additional information, I would have uploaded the file but i'm not sure how to.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Sounds like themain request is to speed up your macro.<o:p></o:p>
<o:p> </o:p>
I don't know ofany built-in functions that can be used to process the data.<o:p></o:p>
<o:p> </o:p>
Turning offdisplay updating will speed things up:<o:p></o:p>
Code:
[SIZE=3][FONT=Times New Roman][COLOR=#000000]Application.ScreenUpdating = False<o:p></o:p>[/COLOR][/FONT][/SIZE]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][SIZE=3][FONT=Times New Roman][COLOR=#000000]Application.ScreenUpdating = True<o:p></o:p>[/COLOR][/FONT][/SIZE]
<o:p> </o:p>
The other speed up I've used is to copy everything into anarray, do all the processing within the array and then write the array backout. In this case you may want to useone array to hold the original data and a second array to hold the results. Here's snippets of code that I've used.<o:p></o:p>
<o:p> </o:p>
Bob<o:p></o:p>
<o:p> </o:p>
Code:
[SIZE=3][FONT=Times New Roman][COLOR=#000000]Dim Sheet_original As Variant<o:p></o:p>[/COLOR][/FONT][/SIZE]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][SIZE=3][FONT=Times New Roman][COLOR=#000000]Dim Sheet_final As Variant<o:p></o:p>[/COLOR][/FONT][/SIZE]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT]<o:p>[FONT=Times New Roman][SIZE=3][COLOR=#000000] [/COLOR][/SIZE][/FONT]</o:p>
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][SIZE=3][FONT=Times New Roman][COLOR=#000000]Sheet_original = Range(Cells(1, 1), Cells(10000,50)).Value   ' copy into array<o:p></o:p>[/COLOR][/FONT][/SIZE]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT]<o:p>[FONT=Times New Roman][SIZE=3][COLOR=#000000] [/COLOR][/SIZE][/FONT]</o:p>
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][SIZE=3][FONT=Times New Roman][COLOR=#000000]original_index_0 = 0<o:p></o:p>[/COLOR][/FONT][/SIZE]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][SIZE=3][FONT=Times New Roman][COLOR=#000000]Do While Sheet_original(original_index_0, 0, 0, 0, 0, …) IsNot Nothing ' could be <> ""<o:p></o:p>[/COLOR][/FONT][/SIZE]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][SIZE=3][FONT=Times New Roman][COLOR=#000000]  Sheet_final(final_index_0, final_index_1, final_index_2, …) =Sheet_original(original_index_0, original_index_1, original_index_2, …)<o:p></o:p>[/COLOR][/FONT][/SIZE]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][SIZE=3][FONT=Times New Roman][COLOR=#000000]original_index_0 = original_index_0 + 1<o:p></o:p>[/COLOR][/FONT][/SIZE]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][SIZE=3][FONT=Times New Roman][COLOR=#000000]Loop<o:p></o:p>[/COLOR][/FONT][/SIZE]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT]<o:p>[FONT=Times New Roman][SIZE=3][COLOR=#000000] [/COLOR][/SIZE][/FONT]</o:p>
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][SIZE=3][FONT=Times New Roman][COLOR=#000000] Sheets("destination_sheet").Select<o:p></o:p>[/COLOR][/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman][COLOR=#000000][FONT="Times New Roman"]Range(Cells(1, 1), Cells(10000, 50)).Value =Sheet_final  'paste array intospreadsheet[/FONT][/COLOR][/FONT][/SIZE]
 
Upvote 0
We'll the main problem is i don't know how to write a macro to accomplish the task at hand. I have written quite a few macros based of recording tasks first, but nothing this extensive.

From what i can tell the macro needs to do the following.

insert 6 columns after % and name the headings producer 2 comission 2 % #2 etc...

find A2 in the invoice column
if the A3 is the same value
then copy B3 and paste in E2.
Then move to A4 if the value = A2
then copy B4 and paste in E2.
Then move to A5 if the value = A2
then copy B5 and paste in E2.


'it would repeat this process till the value in column A no longer matched and would move to the next value or invoice.
Then it would do this process for column C,and D respectively...so on and so forth till the all the data was rolled up into one row.

I would be able to figure out the macro for the totaling of the amount but everything else is beyond me.
I hope this helps explain my situation more.
 
Last edited:
Upvote 0
I suggest you start playing with some macros and looking at examples of VBA code. Nothing like experience to teach you how to do things.

Here's a macro that will do what you want. It's pretty straight forward and shows how to do one type of basic looping.

Code:
Sub report_reformat()

     source_sheet = ActiveSheet.name
     Sheets.Add
     ActiveSheet.name = "result_sheet"
     destination_sheet = ActiveSheet.name
     
' init header row
     Cells(1, 1) = "Invoice"
     Cells(1, 2) = "Amount"
     Cells(1, 3) = "PRD1"
     Cells(1, 4) = "Com1"
     Cells(1, 5) = "PRD 1 %"
     Cells(1, 6) = "PRD2"
     Cells(1, 7) = "Com2"
     Cells(1, 8) = "PRD 2 %"
     Cells(1, 9) = "PRD3"
     Cells(1, 10) = "Com3"
     Cells(1, 11) = "PRD 3 %"
     Cells(1, 12) = "Total Amount"

' make pretty
     Columns("L:L").ColumnWidth = 12.43
     Range("D:D,B:B,G:G,J:J,L:L").Select
     Selection.NumberFormat = "#,##0.00" 
     
     source_row = 2
     destination_row = 2
     
     current_invoice_num = Sheets(source_sheet).Cells(2, 1)
     
     Do While current_invoice_num <> ""
       Cells(destination_row, 1) = current_invoice_num
       Cells(destination_row, 2) = Sheets(source_sheet).Cells(source_row, 5)  'copy amount over
       Cells(destination_row, 12) = Sheets(source_sheet).Cells(source_row, 5)  ' the two amounts are alwats the same
       PRD_num = 1
      
' copy PRD data over
       Do While current_invoice_num = Sheets(source_sheet).Cells(source_row, 1)  ' only move PRD data if in same invoice
         Cells(destination_row, 3 * PRD_num) = Sheets(source_sheet).Cells(source_row, 2)
         Cells(destination_row, 3 * PRD_num + 1) = Sheets(source_sheet).Cells(source_row, 3)
         Cells(destination_row, 3 * PRD_num + 2) = Sheets(source_sheet).Cells(source_row, 4)
         PRD_num = PRD_num + 1
         source_row = source_row + 1
       Loop

' done with this invoice
       destination_row = destination_row + 1
       current_invoice_num = Sheets(source_sheet).Cells(source_row, 1)
     Loop

End Sub

Here's a faster way of doing it. It reads the source sheet into an array, copies the data do a destination array and then writes the destination array into a new sheet.

You'll notice the indices are different between the two. That's because the spreadsheet rows/columns start at 1 while the array indices start at 0.

Bob

Code:
Sub report_reformat_array()

Dim source_array As Variant
Dim destination_array As Variant
Dim temp As Variant

     source_array = Range(Cells(1, 1), ActiveCell.SpecialCells(xlLastCell)).Value  ' read in source sheet
     
     max_x = UBound(source_array, 1) - LBound(source_array, 1) + 1
     ReDim destination_array(max_x, 12)   ' make destination array the same size - that way we know for sure that it's big enough
     
     
     source_row = 2
     destination_row = 0 'array index starts at 0
     
     current_invoice_num = source_array(2, 1)
     
     Do While current_invoice_num <> ""
       destination_array(destination_row, 0) = current_invoice_num
       destination_array(destination_row, 1) = source_array(source_row, 5)  'copy amount over
       destination_array(destination_row, 11) = source_array(source_row, 5)  ' the two amounts are alwats the same
       PRD_num = 1
      
' copy PRD data over
       Do While current_invoice_num = source_array(source_row, 1)  ' only move PRD data if in same invoice
         destination_array(destination_row, 3 * PRD_num - 1) = source_array(source_row, 2)
         destination_array(destination_row, 3 * PRD_num) = source_array(source_row, 3)
         destination_array(destination_row, 3 * PRD_num + 1) = source_array(source_row, 4)
         PRD_num = PRD_num + 1
         source_row = source_row + 1
       Loop

' done with this invoice
       destination_row = destination_row + 1
       current_invoice_num = source_array(source_row, 1)
     Loop

     Sheets.Add
     
' init header row
     Cells(1, 1) = "Invoice"
     Cells(1, 2) = "Amount"
     Cells(1, 3) = "PRD1"
     Cells(1, 4) = "Com1"
     Cells(1, 5) = "PRD 1 %"
     Cells(1, 6) = "PRD2"
     Cells(1, 7) = "Com2"
     Cells(1, 8) = "PRD 2 %"
     Cells(1, 9) = "PRD3"
     Cells(1, 10) = "Com3"
     Cells(1, 11) = "PRD 3 %"
     Cells(1, 12) = "Total Amount"

' make pretty
     Columns("L:L").ColumnWidth = 12.43
     Range("D:D,B:B,G:G,J:J,L:L").Select
     Selection.NumberFormat = "#,##0.00"


     Range(Cells(2, 1), Cells(destination_row + 1, 12)).Value = destination_array 'paste array into spreadsheet
     
     Cells(1, 1).Select

End Sub
 
Upvote 0
Everything seems to be working great, i have been able to adjust the columns to what mjy actual report has. Although iv'e run into an issue. The rule set up was to not copy over a producer if the amount is repeated. sometimes and invoice is back out and re done. this means the invoice may have 24 then -24 then 24 again. How would the code be adjusted to only take the absolute value and not tell the difference between + and - numbers?
 
Upvote 0
For an example look at the below.

1768 prod13-2.8410284-284
1768 BRB0000
1768 prod132.8410284284
1768 BRB0000

<colgroup><col span="2"><col><col span="3"></colgroup><tbody>
</tbody>
 
Upvote 0
What does the final result look like?

Will all the invoice rows be adjacent or will I need to do some type of sort/search?

Bob
 
Upvote 0
What does the final result look like?

Will all the invoice rows be adjacent or will I need to do some type of sort/search?

Bob

the result would be
1768 prod13
-2.8410
BRB00

<colgroup><col span="8" width="64"></colgroup><tbody>
</tbody>
with a total of 0.
i thought i would be able to figure out how to total up the vlues of amounts but i'm having trouble with that as well :(
 
Upvote 0

Forum statistics

Threads
1,216,750
Messages
6,132,505
Members
449,730
Latest member
SeanHT

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