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
315 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
[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
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
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!

 
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

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!

Question has a verified solution.

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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
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 Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

726 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