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
309 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

860 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