Macro to split data by divisons and average amounts

Calig

New Member
Joined
Apr 23, 2011
Messages
1
Hi,

It's kind of hard to come up with an appropriate title for what I want to do. I'm relatively new with VBA and I am trying to solve an issue. Sorry if the following data is a bit messy, but this is how it's suppose to end up looking like.


<table style="border-collapse: collapse; width: 412pt;" border="0" cellpadding="0" cellspacing="0" width="550"><col style="width: 48pt;" width="64" span="2"> <col style="width: 53pt;" width="71"> <col style="width: 48pt;" width="64"> <col style="width: 54pt;" width="72"> <col style="width: 59pt;" width="79"> <col style="width: 48pt;" width="64"> <col style="width: 54pt;" width="72"> <tbody><tr style="height: 12.75pt;" height="17"> <td class="xl67" style="height: 12.75pt; width: 48pt;" width="64" height="17">Company</td> <td style="width: 48pt;" width="64">Division</td> <td class="xl68" style="width: 53pt;" width="71">Date</td> <td class="xl69" style="width: 48pt;" width="64">Invoice</td> <td class="xl70" style="width: 54pt;" width="72">Amount</td> <td style="width: 59pt;" width="79">
</td> <td class="xl74" style="width: 48pt;" width="64">Number </td> <td class="xl75" style="width: 54pt;" width="72">Average</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl67" style="height: 12.75pt;" height="17">FP&L</td> <td>A</td> <td class="xl71" align="right">01/24/2005</td> <td align="right">2046</td> <td class="xl72"> 9,919.65 </td> <td>
</td> <td>
</td> <td class="xl66">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl67" style="height: 12.75pt;" height="17">FP&L</td> <td>A</td> <td class="xl71" align="right">04/24/2005</td> <td align="right">2202</td> <td class="xl72"> 10,187.77 </td> <td>
</td> <td>
</td> <td class="xl66">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl67" style="height: 12.75pt;" height="17">FP&L</td> <td>A</td> <td class="xl71" align="right">10/26/2005</td> <td align="right">2655</td> <td class="xl72"> 11,534.81 </td> <td>
</td> <td>
</td> <td class="xl66">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl67" style="height: 12.75pt;" height="17">FP&L</td> <td>A</td> <td class="xl71" align="right">10/31/2005</td> <td align="right">2726</td> <td class="xl72"> 11,666.62 </td> <td class="xl72"> 43,308.85 </td> <td class="xl69">4</td> <td class="xl76"> 10,827.21 </td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl67" style="height: 12.75pt;" height="17">FP&L</td> <td>B</td> <td class="xl71" align="right">02/20/2005</td> <td align="right">2076</td> <td class="xl72"> 9,744.21 </td> <td>
</td> <td class="xl69">
</td> <td class="xl66">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl67" style="height: 12.75pt;" height="17">FP&L</td> <td>B</td> <td class="xl71" align="right">04/08/2005</td> <td align="right">2154</td> <td class="xl72"> 10,143.71 </td> <td>
</td> <td class="xl69">
</td> <td class="xl66">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl67" style="height: 12.75pt;" height="17">FP&L</td> <td>B</td> <td class="xl71" align="right">10/21/2005</td> <td align="right">2584</td> <td class="xl72"> 11,289.24 </td> <td>
</td> <td class="xl69">
</td> <td class="xl66">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl67" style="height: 12.75pt;" height="17">FP&L</td> <td>B</td> <td class="xl71" align="right">11/19/2005</td> <td align="right">2868</td> <td class="xl72"> 12,124.01 </td> <td class="xl72"> 43,301.17 </td> <td class="xl69">4</td> <td class="xl76"> 10,825.29 </td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl67" style="height: 12.75pt;" height="17">FP&L</td> <td>C</td> <td class="xl71" align="right">02/05/2005</td> <td align="right">2061</td> <td class="xl72"> 9,764.32 </td> <td>
</td> <td class="xl69">
</td> <td class="xl66">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl67" style="height: 12.75pt;" height="17">FP&L</td> <td>C</td> <td class="xl71" align="right">05/12/2005</td> <td align="right">2250</td> <td class="xl72"> 10,431.84 </td> <td>
</td> <td class="xl69">
</td> <td class="xl66">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl67" style="height: 12.75pt;" height="17">FP&L</td> <td>C</td> <td class="xl71" align="right">06/27/2005</td> <td align="right">2346</td> <td class="xl72"> 10,819.97 </td> <td>
</td> <td class="xl69">
</td> <td class="xl66">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl67" style="height: 12.75pt;" height="17">FP&L</td> <td>C</td> <td class="xl71" align="right">12/10/2005</td> <td align="right">3010</td> <td class="xl72"> 12,712.64 </td> <td class="xl72"> 43,728.77 </td> <td class="xl69">4</td> <td class="xl76"> 10,932.19 </td> </tr> <tr style="height: 13.5pt;" height="18"> <td class="xl67" style="height: 13.5pt;" height="18">
</td> <td>
</td> <td class="xl71">
</td> <td>
</td> <td class="xl72">
</td> <td class="xl72">
</td> <td class="xl69">
</td> <td class="xl66">
</td> </tr> <tr style="height: 14.25pt;" height="19"> <td class="xl67" style="height: 14.25pt;" height="19">Total</td> <td>FP&L</td> <td class="xl71">
</td> <td>
</td> <td class="xl72">
</td> <td class="xl65"> 130,338.79 </td> <td class="xl69">
</td> <td class="xl66">
</td> </tr> <tr style="height: 13.5pt;" height="18"> <td class="xl67" style="height: 13.5pt;" height="18">
</td> <td>
</td> <td class="xl71">
</td> <td>
</td> <td class="xl72">
</td> <td class="xl73">
</td> <td class="xl69">
</td> <td class="xl66">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl67" style="height: 12.75pt;" height="17">Miami Dade</td> <td>AA</td> <td class="xl71" align="right">03/07/2005</td> <td align="right">2091</td> <td class="xl72"> 9,879.35 </td> <td class="xl73">
</td> <td class="xl69">
</td> <td class="xl66">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl67" style="height: 12.75pt;" height="17">Miami Dade</td> <td>AA</td> <td class="xl71" align="right">11/14/2005</td> <td align="right">2797</td> <td class="xl72"> 11,998.44 </td> <td class="xl73">
</td> <td class="xl69">
</td> <td class="xl66">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl67" style="height: 12.75pt;" height="17">Miami Dade</td> <td>AA</td> <td class="xl71" align="right">12/05/2005</td> <td align="right">2939</td> <td class="xl72"> 12,532.57 </td> <td class="xl73"> 34,410.36 </td> <td class="xl69">3</td> <td class="xl76"> 11,470.12 </td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl67" style="height: 12.75pt;" height="17">Miami Dade</td> <td>BB</td> <td class="xl71" align="right">01/15/2005</td> <td align="right">2016</td> <td class="xl72"> 9,554.21 </td> <td class="xl73">
</td> <td class="xl69">
</td> <td class="xl66">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl67" style="height: 12.75pt;" height="17">Miami Dade</td> <td>BB</td> <td class="xl71" align="right">03/25/2005</td> <td align="right">2106</td> <td class="xl72"> 9,849.98 </td> <td class="xl73">
</td> <td class="xl69">
</td> <td class="xl66">
</td> </tr> <tr style="height: 13.5pt;" height="18"> <td class="xl67" style="height: 13.5pt;" height="18">Miami Dade</td> <td>BB</td> <td class="xl71" align="right">07/21/2005</td> <td align="right">2394</td> <td class="xl72"> 10,676.54 </td> <td class="xl73"> 30,080.73 </td> <td class="xl69">3</td> <td class="xl76"> 10,026.91 </td> </tr> <tr style="height: 14.25pt;" height="19"> <td class="xl67" style="height: 14.25pt;" height="19">Total</td> <td colspan="2" style="">Miami Dade</td> <td>
</td> <td class="xl72">
</td> <td class="xl65"> 64,491.09 </td> <td class="xl69">
</td> <td class="xl66">
</td> </tr> <tr style="height: 13.5pt;" height="18"> <td class="xl67" style="height: 13.5pt;" height="18">
</td> <td>
</td> <td class="xl71">
</td> <td>
</td> <td class="xl72">
</td> <td class="xl73">
</td> <td class="xl69">
</td> <td class="xl66">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl67" style="height: 12.75pt;" height="17">Pearson</td> <td>BBB</td> <td class="xl71" align="right">06/03/2005</td> <td align="right">2298</td> <td class="xl72"> 10,388.41 </td> <td class="xl73"> 10,388.41 </td> <td class="xl69">1</td> <td class="xl76"> 10,388.41 </td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl67" style="height: 12.75pt;" height="17">Pearson</td> <td>CCC</td> <td class="xl71" align="right">01/18/2005</td> <td align="right">2031</td> <td class="xl72"> 9,799.35 </td> <td class="xl73">
</td> <td class="xl69">
</td> <td class="xl66">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl67" style="height: 12.75pt;" height="17">Pearson</td> <td>CCC</td> <td class="xl71" align="right">08/14/2005</td> <td align="right">2442</td> <td class="xl72"> 10,933.11 </td> <td class="xl73"> 20,732.46 </td> <td class="xl69">2</td> <td class="xl76"> 10,366.23 </td> </tr> <tr style="height: 13.5pt;" height="18"> <td class="xl67" style="height: 13.5pt;" height="18">Pearson</td> <td>DDD</td> <td class="xl71" align="right">09/07/2005</td> <td align="right">2513</td> <td class="xl72"> 11,246.17 </td> <td class="xl73"> 11,246.17 </td> <td class="xl69">1</td> <td class="xl76"> 11,246.17 </td> </tr> <tr style="height: 14.25pt;" height="19"> <td class="xl67" style="height: 14.25pt;" height="19">Total</td> <td>Pearson</td> <td class="xl71">
</td> <td>
</td> <td class="xl72">
</td> <td class="xl65"> 42,367.04 </td> <td>
</td> <td class="xl66">
</td> </tr> <tr style="height: 14.25pt;" height="19"> <td class="xl67" style="height: 14.25pt;" height="19">
</td> <td>
</td> <td class="xl71">
</td> <td>
</td> <td class="xl72">
</td> <td class="xl73">
</td> <td>
</td> <td class="xl66">
</td> </tr> <tr style="height: 14.25pt;" height="19"> <td class="xl67" colspan="2" style="height: 14.25pt;" height="19">Grand Total</td> <td class="xl71">
</td> <td>
</td> <td class="xl72">
</td> <td class="xl65"> 237,196.92 </td> <td align="right">22</td> <td class="xl76"> 10,781.68 </td> </tr> </tbody></table>






I wrote a macro that will get the subtotals of amounts by divison (A, B, C, etc...), but then I'm stuck when I'm trying to get it to count the number of A's, B's, etc so it could then make a number column. For example, if I add two more A's to the data, the number 6 will appear under the number column. Then it will start counting the number of B's, and put a 4 at the end of the data (because there are 4 B's), and start again with C.


Here is the code I used to do the subtotals for the amounts related to each division. I am trying to adapt it in some way so it counts the number of A's, B's, and C's as mentioned previously. I don't know if it's in any way useful.


<table style="border-collapse: collapse; width: 412pt;" border="0" cellpadding="0" cellspacing="0" width="550"><col style="width: 48pt;" width="64" span="2"> <col style="width: 53pt;" width="71"> <col style="width: 48pt;" width="64"> <col style="width: 54pt;" width="72"> <col style="width: 59pt;" width="79"> <col style="width: 48pt;" width="64"> <col style="width: 54pt;" width="72"> <tbody><tr style="height: 12.75pt;" height="17"> <td class="xl65" style="height: 12.75pt; width: 48pt;" width="64" height="17">Application.Goto Reference:="Division"

ActiveCell.Offset(1, 0).Range("A1").Select
Do While ActiveCell <> ""
MydivisionSales = 0
Mydivision = ActiveCell
Do While ActiveCell = Mydivision
ActiveCell.Offset(0, 3).Range("A1").Select
If ActiveCell > 0 Then
MydivisionSales = MydivisionSales + ActiveCell
ActiveCell.Offset(1, 0).Range("A1").Select

Else

'Do Nothing

End If

ActiveCell.Offset(0, -3).Range("A1").Select

Loop


'Temp1 Macro
ActiveCell.Offset(-1, 4).Range("A1").Select

ActiveCell.FormulaR1C1 = "=SUM(R[-12]C[-1]:R[-1]C[-1])"
ActiveCell.FormulaR1C1 = MydivisionSales
ActiveCell.Select
Selection.Style = "Comma"
Selection.Style = "Currency"
ActiveCell.Offset(1, -4).Range("A1").Select
'End Temp1 Macro

Loop


ActiveCell.Offset(3, 0).Range("A1").Select

Do While ActiveCell <> ""
MydivisionSales = 0
Mydivision = ActiveCell
Do While ActiveCell = Mydivision
ActiveCell.Offset(0, 3).Range("A1").Select
If ActiveCell > 0 Then
MydivisionSales = MydivisionSales + ActiveCell
ActiveCell.Offset(1, 0).Range("A1").Select

Else

'Do Nothing

End If

ActiveCell.Offset(0, -3).Range("A1").Select

Loop


'Temp1 Macro
ActiveCell.Offset(-1, 4).Range("A1").Select

ActiveCell.FormulaR1C1 = "=SUM(R[-12]C[-1]:R[-1]C[-1])"
ActiveCell.FormulaR1C1 = MydivisionSales
ActiveCell.Select
Selection.Style = "Comma"
Selection.Style = "Currency"
ActiveCell.Offset(1, -4).Range("A1").Select
'End Temp1 Macro

Loop

ActiveCell.Offset(3, 0).Range("A1").Select

Do While ActiveCell <> ""
MydivisionSales = 0
Mydivision = ActiveCell
Do While ActiveCell = Mydivision
ActiveCell.Offset(0, 3).Range("A1").Select
If ActiveCell > 0 Then
MydivisionSales = MydivisionSales + ActiveCell
ActiveCell.Offset(1, 0).Range("A1").Select

Else

'Do Nothing

End If

ActiveCell.Offset(0, -3).Range("A1").Select

Loop


'Temp1 Macro
ActiveCell.Offset(-1, 4).Range("A1").Select

ActiveCell.FormulaR1C1 = "=SUM(R[-12]C[-1]:R[-1]C[-1])"
ActiveCell.FormulaR1C1 = MydivisionSales
ActiveCell.Select
Selection.Style = "Comma"
Selection.Style = "Currency"
ActiveCell.Offset(1, -4).Range("A1").Select
'End Temp1 Macro

Loop


End Sub
</td> <td style="width: 48pt;" width="64">
</td> <td class="xl66" style="width: 53pt;" width="71">
</td> <td class="xl67" style="width: 48pt;" width="64">
</td> <td class="xl68" style="width: 54pt;" width="72">
</td> <td style="width: 59pt;" width="79">
</td> <td class="xl72" style="width: 48pt;" width="64">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl65" style="height: 12.75pt;" height="17">
</td> <td>
</td> <td class="xl69" align="right">
</td> <td align="right">
</td> <td class="xl70">
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl65" style="height: 12.75pt;" height="17">
</td> <td>
</td> <td class="xl69" align="right">
</td> <td align="right">
</td> <td class="xl70">
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl65" style="height: 12.75pt;" height="17">
</td> <td>
</td> <td class="xl69" align="right">
</td> <td align="right">
</td> <td class="xl70">
</td> <td>
</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl65" style="height: 12.75pt;" height="17">Sorry for the long post! Thanks!!!
</td> <td>
</td> <td class="xl69" align="right">
</td> <td align="right">
</td> <td class="xl70">
</td> <td class="xl70">
</td> <td class="xl67">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl65" style="height: 12.75pt;" height="17">
</td> <td>
</td> <td class="xl69" align="right">
</td> <td align="right">
</td> <td class="xl70">
</td> <td>
</td> <td class="xl67">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl65" style="height: 12.75pt;" height="17">
</td> <td>
</td> <td class="xl69" align="right">
</td> <td align="right">
</td> <td class="xl70">
</td> <td>
</td> <td class="xl67">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl65" style="height: 12.75pt;" height="17">
</td> <td>
</td> <td class="xl69" align="right">
</td> <td align="right">
</td> <td class="xl70">
</td> <td>
</td> <td class="xl67">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl65" style="height: 12.75pt;" height="17">
</td> <td>
</td> <td class="xl69" align="right">
</td> <td align="right">
</td> <td class="xl70">
</td> <td class="xl70">
</td> <td class="xl67">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl65" style="height: 12.75pt;" height="17">
</td> <td>
</td> <td class="xl69" align="right">
</td> <td align="right">
</td> <td class="xl70">
</td> <td>
</td> <td class="xl67">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl65" style="height: 12.75pt;" height="17">
</td> <td>
</td> <td class="xl69" align="right">
</td> <td align="right">
</td> <td class="xl70">
</td> <td>
</td> <td class="xl67">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl65" style="height: 12.75pt;" height="17">
</td> <td>
</td> <td class="xl69" align="right">
</td> <td align="right">
</td> <td class="xl70">
</td> <td>
</td> <td class="xl67">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl65" style="height: 12.75pt;" height="17">
</td> <td>
</td> <td class="xl69" align="right">
</td> <td align="right">
</td> <td class="xl70">
</td> <td class="xl70">
</td> <td class="xl67">
</td> </tr> <tr style="height: 13.5pt;" height="18"> <td class="xl65" style="height: 13.5pt;" height="18">
</td> <td>
</td> <td class="xl69">
</td> <td>
</td> <td class="xl70">
</td> <td class="xl70">
</td> <td class="xl67">
</td> </tr> <tr style="height: 14.25pt;" height="19"> <td class="xl65" style="height: 14.25pt;" height="19">
</td> <td>
</td> <td class="xl69">
</td> <td>
</td> <td class="xl70">
</td> <td class="xl63">
</td> <td class="xl67">
</td> </tr> <tr style="height: 13.5pt;" height="18"> <td class="xl65" style="height: 13.5pt;" height="18">
</td> <td>
</td> <td class="xl69">
</td> <td>
</td> <td class="xl70">
</td> <td class="xl71">
</td> <td class="xl67">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl65" style="height: 12.75pt;" height="17">
</td> <td>
</td> <td class="xl69" align="right">
</td> <td align="right">
</td> <td class="xl70">
</td> <td class="xl71">
</td> <td class="xl67">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl65" style="height: 12.75pt;" height="17">
</td> <td>
</td> <td class="xl69" align="right">
</td> <td align="right">
</td> <td class="xl70">
</td> <td class="xl71">
</td> <td class="xl67">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl65" style="height: 12.75pt;" height="17">
</td> <td>
</td> <td class="xl69" align="right">
</td> <td align="right">
</td> <td class="xl70">
</td> <td class="xl71">
</td> <td class="xl67">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl65" style="height: 12.75pt;" height="17">
</td> <td>
</td> <td class="xl69" align="right">
</td> <td align="right">
</td> <td class="xl70">
</td> <td class="xl71">
</td> <td class="xl67">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl65" style="height: 12.75pt;" height="17">
</td> <td>
</td> <td class="xl69" align="right">
</td> <td align="right">
</td> <td class="xl70">
</td> <td class="xl71">
</td> <td class="xl67">
</td> </tr> <tr style="height: 13.5pt;" height="18"> <td class="xl65" style="height: 13.5pt;" height="18">
</td> <td>
</td> <td class="xl69" align="right">
</td> <td align="right">
</td> <td class="xl70">
</td> <td class="xl71">
</td> <td class="xl67">
</td> </tr> <tr style="height: 14.25pt;" height="19"> <td class="xl65" style="height: 14.25pt;" height="19">
</td> <td colspan="2" style="">
</td> <td>
</td> <td class="xl70">
</td> <td class="xl63">
</td> <td class="xl67">
</td> </tr> <tr style="height: 13.5pt;" height="18"> <td class="xl65" style="height: 13.5pt;" height="18">
</td> <td>
</td> <td class="xl69">
</td> <td>
</td> <td class="xl70">
</td> <td class="xl71">
</td> <td class="xl67">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl65" style="height: 12.75pt;" height="17">
</td> <td>
</td> <td class="xl69" align="right">
</td> <td align="right">
</td> <td class="xl70">
</td> <td class="xl71">
</td> <td class="xl67">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl65" style="height: 12.75pt;" height="17">
</td> <td>
</td> <td class="xl69" align="right">
</td> <td align="right">
</td> <td class="xl70">
</td> <td class="xl71">
</td> <td class="xl67">
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td class="xl65" style="height: 12.75pt;" height="17">
</td> <td>
</td> <td class="xl69" align="right">
</td> <td align="right">
</td> <td class="xl70">
</td> <td class="xl71">
</td> <td class="xl67">
</td> </tr> <tr style="height: 13.5pt;" height="18"> <td class="xl65" style="height: 13.5pt;" height="18">
</td> <td>
</td> <td class="xl69" align="right">
</td> <td align="right">
</td> <td class="xl70">
</td> <td class="xl71">
</td> <td class="xl67">
</td> </tr> <tr style="height: 14.25pt;" height="19"> <td class="xl65" style="height: 14.25pt;" height="19">
</td> <td>
</td> <td class="xl69">
</td> <td>
</td> <td class="xl70">
</td> <td class="xl63">
</td> <td>
</td> </tr> <tr style="height: 14.25pt;" height="19"> <td class="xl65" style="height: 14.25pt;" height="19">
</td> <td>
</td> <td class="xl69">
</td> <td>
</td> <td class="xl70">
</td> <td class="xl71">
</td> <td>
</td> </tr> </tbody></table>
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try this to return columns "F to H".
Code:
[COLOR=navy]Sub[/COLOR] MG23Apr15
[COLOR=navy]Dim[/COLOR] Rng [COLOR=navy]As[/COLOR] Range, Dn [COLOR=navy]As[/COLOR] Range
[COLOR=navy]Dim[/COLOR] tot [COLOR=navy]As[/COLOR] Double
[COLOR=navy]Dim[/COLOR] gTot [COLOR=navy]As[/COLOR] Double
[COLOR=navy]Dim[/COLOR] ggTot [COLOR=navy]As[/COLOR] Double
[COLOR=navy]Dim[/COLOR] c [COLOR=navy]As[/COLOR] [COLOR=navy]Integer[/COLOR]
[COLOR=navy]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & rows.Count).End(xlUp))
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng
    [COLOR=navy]If[/COLOR] Dn = "Grand Total" [COLOR=navy]Then[/COLOR]
        Dn.Offset(, 5) = Format(ggTot, "#,###.##")
    [COLOR=navy]End[/COLOR] If
    [COLOR=navy]If[/COLOR] Dn = "Total" [COLOR=navy]Then[/COLOR]
        Dn.Offset(, 5) = Format(gTot, "#,###.##")
        ggTot = ggTot + gTot
        gTot = 0
    [COLOR=navy]End[/COLOR] If
        tot = tot + Dn.Offset(, 4)
        c = c + 1
    [COLOR=navy]If[/COLOR] Dn.Offset(, 1) <> Dn.Offset(1, 1) And tot <> 0 [COLOR=navy]Then[/COLOR]
        Dn.Offset(, 5) = Format(tot, "#,###.##")
        Dn.Offset(, 6) = c
        Dn.Offset(, 7) = Format(tot / c, "#,###.##")
        gTot = gTot + tot
        tot = 0
        c = 0
    [COLOR=navy]End[/COLOR] If
[COLOR=navy]Next[/COLOR] Dn
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,845
Members
452,948
Latest member
UsmanAli786

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