Solved

In MS EXCEL VBA, I want to dynamically calculate count,sum,avg for a row as its added

Posted on 2011-03-08
6
306 Views
Last Modified: 2012-05-11
In column A I have a list of names.  Columns B,C,D have AVG,SUM,COUNT.  COUNT and SUM get their values from column E,F,G,H and so on. Column B calculates the AVG from SUM and COUNT.   From time to time I add info to the sheet which includes names that already exist in column A and add new names if they don't exist in column A.  I would like to dynamically add the sum,avg,count to B,C,D as the sheet is updated.  I know this can be done with a cell formula but I don't want cells to show #DIV/0! or zeros if there is no information for them.  Can this be done with VBA instead?  here is sample vba:

      For y = 8 To last_cell
                If player_name = Worksheets("Current Year").Cells(y, 1) Then
                     Worksheets("Current Year").Cells(y, 5).Value = position
                     
                  '   what i would like to calculate
                      Worksheets("Current Year").Cells(y, 2).Value = AVG
                      Worksheets("Current Year").Cells(y, 3).Value = SUM
                      Worksheets("Current Year").Cells(y, 4).Value = COUNT of range ' E  "y" '  to  so on


                     player_exists = True
                End If
0
Comment
Question by:dmalovich
6 Comments
 
LVL 6

Expert Comment

by:Eric Zwiekhorst
ID: 35069642
dear Dmalovich,

it can be done with formula and without error like #DIV/0

cellAVG = =IF( ISERROR(AVERAGE(E1:H1)),0,AVERAGE(E1:H1))
cellSUM= =SUM(E1:H1)
cell Count= =COUNTIF(E1:H1;">0")

instead of error
0
 
LVL 6

Accepted Solution

by:
royhsiao earned 500 total points
ID: 35069660
Try this
Sub Test()
Dim Player_total_count As Integer
Dim i As Integer

Player_total_count = Application.WorksheetFunction.CountA(Range("A1:A2000"))
i = 2

For i = 2 To Player_total_count
    
    Worksheets("Current Year").Cells(i, 2).Value = "=average(E" & i & ":" & "H" & i & ")"
    Worksheets("Current Year").Cells(i, 3).Value = "=sum(E" & i & ":" & "H" & i & ")"
    Worksheets("Current Year").Cells(i, 4).Value = "=count(E" & i & ":" & "H" & i & ")"

Next i
End Sub

Open in new window

0
 
LVL 6

Expert Comment

by:royhsiao
ID: 35069743
The cell shows #DIV/0 in avg. when the count is 0. I could add an if statement to avoid that error. here is an example.
Sub Test()
Dim Player_total_count As Integer
Dim i As Integer
Dim y As Integer

Player_total_count = Application.WorksheetFunction.CountA(Range("A1:A2000"))
i = 2

For i = 2 To Player_total_count
    y = Application.WorksheetFunction.CountA(Range("E" & i & ":" & "H" & i))
    
    If y = 0 Then
    Worksheets("Current Year").Cells(i, 3).Value = "=sum(E" & i & ":" & "H" & i & ")"
    Worksheets("Current Year").Cells(i, 4).Value = "=count(E" & i & ":" & "H" & i & ")"
    ElseIf y <> 0 Then
    Worksheets("Current Year").Cells(i, 2).Value = "=average(E" & i & ":" & "H" & i & ")"
    Worksheets("Current Year").Cells(i, 3).Value = "=sum(E" & i & ":" & "H" & i & ")"
    Worksheets("Current Year").Cells(i, 4).Value = "=count(E" & i & ":" & "H" & i & ")"
    End If

Next i
End Sub

Open in new window

0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 9

Expert Comment

by:dmang
ID: 35069765
Hi there
I have attached a workbook that contains macro code that should provide what you're looking for.
There are sample columns and values provided.
Each row's avg, sum, count are calculated based on the row contents..that is, in one row you can have 6 cols of values, in another - 8, etc

Run the macro calcall.

Regards
dmang 26871049.xls
0
 

Author Closing Comment

by:dmalovich
ID: 35069789
It worked.  Thanks.....
0
 
LVL 1

Expert Comment

by:martyk
ID: 35069856

You can use formulas and avoid the display of #DIV/0 and zeros without resorting to a macro.

In column D, the COUNT column, use
=if(count(e1:z1)>0,count(e1:z1),"")

Column D will be blank if all the columns E to Z are empty.

Then columns B and C can use the D column result to avoid the unwanted display.

In Column B, the AVG column, use the formula
=if(and(d1<>"" , d1>0),c1/d1,"")

And in column C, the SUM column
=if(and(d1<>"" , d1>0),sum(e1:z1),"")

Note you must check both d1<>"" and d1>0 for the formula to work.

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

776 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