Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Excel macro for sum on dynamic range

Posted on 2010-11-21
8
Medium Priority
?
1,189 Views
Last Modified: 2012-05-10
Hi,
We have a column A (starting in cell A10) containing Customer IDs.
There's a column C containing different amounts associated with those Customer IDs.
We need to add total sums per Customer IDs at the last line of a listed Customer ID. See an example attached (number of Customers/rows keeps changing - is dynamic).

Then we need (per every CustomerID) to add a percentage amount field and highlight the top amounts which contribute to 80% or more of the all charged amounts (as shown in the sample).
Thank you.
J.
CutomerIDs-amounts.xlsx
0
Comment
Question by:janime
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 23

Expert Comment

by:Michael Fowler
ID: 34182651
This code should solve the issue for you. I have attached the workbook with the macro included as well
Sub calc()
   Dim i As Long, custID As String, amount As Double, percent As Double
   i = 10
   custID = Range("A" & i).Value
   While Range("A" & i).Value <> ""
      If custID = Range("A" & i).Value Then
         amount = amount + Range("C" & i).Value
         percent = percent + Range("D" & i).Value
         If percent >= 0.8 Then
            Range("E" & i).Value = percent
            percent = 0
         End If
         If custID <> Range("A" & i + 1).Value Then Range("E" & i).Value = amount
      Else
         custID = Range("A" & i).Value
         amount = Range("C" & i).Value
         percent = Range("D" & i).Value
         If percent >= 0.8 Then
            Range("E" & i).Value = percent
            percent = 0
         End If
      End If
      i = i + 1
   Wend
End Sub

Open in new window

CutomerIDs-amounts-1.xlsm
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34182677
Alternatively how about a formula to do it for you?

i.e. in cell: F10 paste the following and drag down:

=IF(COUNTIF(A$10:A$1000,A10)=COUNTIF(A$10:A10,A10),SUMIF(A$10:A10,A10,C$10:C10),"")

Chris
0
 
LVL 50
ID: 34182760
Hello,

without any VBA, this formula

=IF(AND(SUMPRODUCT(--($A9:A$10=A10),$D9:D$10)<0.8,SUMPRODUCT(--($A$10:A10=A10),$D$10:D10)>0.8),SUMPRODUCT(--($A$10:A10=A10),$D$10:D10),IF(A11<>A10,SUMPRODUCT(($A$10:A10=A10)*($C$10:C10)),""))

will identify the first percentage sum greater than 80% and will sum the total per Customer ID in the same column.

You can then use conditional formatting with the formula rule

=OR(SUMPRODUCT(--($A$10:$A10=$A10),$D$10:$D10)<0.8,ISNUMBER($G10))

to apply the cell shading.

see attached.

cheers, teylyn
Copy-of-CutomerIDs-amounts.xlsx
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:janime
ID: 34182956
Hi,
thanks for your replies, none of them however ADDS a new column with recalculated percentage (as per my sample). It seems that your scenarios take into account that the amount percentage is already there (recalculated).
"we need (per every CustomerID) to add a percentage amount field and highlight the top amounts which contribute to 80% or more of the all charged amounts".
I guess this can be accommodated by macro only.
0
 
LVL 50

Accepted Solution

by:
Ingeborg Hawighorst (Microsoft MVP / EE MVE) earned 2000 total points
ID: 34184290
OK,

see the attached workbook. The macro first creates the sum total for each customer in column E, then enters the formula to calculate the percentages in column D, then identifies the cell where the running total of the percentage goes over 80% and formats this and the rows above with a background fill.

cheers, teylyn
Copy-of-CutomerIDs-amounts-1.xlsm
0
 
LVL 23

Expert Comment

by:Michael Fowler
ID: 34184311
Janime

Does this meet your needs

 CutomerIDs-amounts-1.xlsm
Sub calc()
   Dim i As Long, custID As String, amount As Double, percent As Double, row As Long
   i = 10
   row = 10
   custID = Range("A" & i).Value
   While Range("A" & i).Value <> ""
      If custID = Range("A" & i).Value Then
         amount = amount + Range("C" & i).Value
         If custID <> Range("A" & i + 1).Value Then
            Range("E" & i).Value = amount
            Range("E" & i).NumberFormat = "#,##0.00"
            Range("A" & i & ":E" & i).Borders(xlEdgeBottom).Weight = xlMedium
            Call percentages(row, i, amount)
         End If
      Else
         custID = Range("A" & i).Value
         amount = Range("C" & i).Value
         percent = Range("D" & i).Value
         row = i
      End If
      i = i + 1
   Wend
End Sub

Sub percentages(startRow As Long, endRow As Long, amount As Double)
   Dim i As Long, percent As Double, highlighted As Boolean
   highlighted = False
   For i = startRow To endRow
      Range("D" & i).Value = Range("C" & i).Value / amount
      Range("D" & i).NumberFormat = "0.00%"
      percent = percent + Range("D" & i).Value
      If percent >= 0.8 And Not highlighted Then
            Range("E" & i).Value = percent
            Range("E" & i).NumberFormat = "0.00%"
            Range("A" & startRow & ":D" & i).Interior.Color = 10092543
            Range("A" & i & ":E" & i).Borders(xlEdgeBottom).Weight = xlThin
            highlighted = True
      End If
   Next
End Sub

Open in new window

0
 

Author Closing Comment

by:janime
ID: 34184721
Excellent! Thank you.
0
 

Author Comment

by:janime
ID: 34184740
Sorry, Michael74, I already gave points to teylyn. I did not notice your post. His macro did the work. So hope next time I'll compensate you for your effort. Thanks once again.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question