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
304 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

744 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

12 Experts available now in Live!

Get 1:1 Help Now