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
305 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

919 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now