Link to home
Start Free TrialLog in
Avatar of PRAVEEN T
PRAVEEN TFlag for United States of America

asked on

Need help on Excel Macro to get sum,avg and result

Hi ,
Please help me to create excel marco for attached excel sheet.
test.xlsx
Avatar of Karen Schaefer
Karen Schaefer
Flag of United States of America image

What are you trying to accomplish?
Need details, please.
Avatar of PRAVEEN T

ASKER

Hi,
I have the excel sheet with the following columns
studentID
Mark_1
Mark_2
Mark_3
Total
AVG
Result

I need a macro that can get values ( Mark_1,2,3) from the sheet and update the Total , Average and Result

Result :: Pass/Fail
If avegrate < 50 then fail else pass

Pass - column should fill with green
Fail - Column should fill with red..

All  things need to done in the macro only...

Thanks
how do you want to call the macro, a button or on enter of a cell, or on open  of the worksheet?
are you familiar with Pivot tables?
or you could use calculated functions on necessary cells.
I need to call this macro with button click
Avatar of Shums Faruk
Do you mean like attached?

Why do you need macro?
Praveen_Formula.xlsx
Shums approach is exactly where I was going.  It is the best approach.

K
I know we can do it by aggregate function with excel provided , But we are working on the reports where we are getting table data to excel...in the macro we need to handle these things.. user dont need to select from the excel shseet. ( Ijust want to automate this process)
Ok Try below Macro:
Hope you have Sheet name as "Test", and columns headers are same:
Sub UpdateResult()
Dim Ws As Worksheet
Application.ScreenUpdating = False
Set Ws = Worksheets("Test")

With Ws.ListObjects(1)
    .ListColumns("Total").DataBodyRange.FormulaR1C1 = "=SUM(Table1[@[Mark_1]:[Mark_3]])"
    .ListColumns("AVG").DataBodyRange.FormulaR1C1 = "=AVERAGE(Table1[@[Mark_1]:[Mark_3]])"
    .ListColumns("Result").DataBodyRange.FormulaR1C1 = "=IF([@AVG]<50,""Fail"",""Pass"")"
End With
With Ws.ListObjects(1).ListColumns("Result").DataBodyRange
    .FormatConditions.Delete
    .FormatConditions.Add xlCellValue, Operator:=xlEqual, Formula1:="=""Pass"""
    .FormatConditions(1).Interior.Color = RGB(0, 192, 0)
    .FormatConditions.Add xlCellValue, Operator:=xlEqual, Formula1:="=""Fail"""
    .FormatConditions(2).Interior.Color = RGB(255, 0, 0)
End With

Application.ScreenUpdating = True
End Sub

Open in new window

Try in attached first before applying on your original.
Praveen_Formula_v2.xlsm
Thank you . this is working fine.

I need a small change on that.  Can we add the range that user can enter the value then click on the button.. the macro is going to update the those rows that user entered.

Example::

If the user enter the student ID 1 to 5... then only those records need to update with the macro...

Can we do this

Thank you.
Are you gonna have table range or cell range?

If its gonna be table range, then it would be difficult.
Cell range.. like student id..1 to 5
In your initial sample you provided with Table, will that range be Table Range or Normal Range?
Its normal range
Sorry then I would request you to close this question and raise another question. Above vba I provided was for Table Range which you asked initially and provided sample with Table Range.
Sure
Hi ,
I have raise the new case .. Can you please help me on this ... Thank you
ASKER CERTIFIED SOLUTION
Avatar of Shums Faruk
Shums Faruk
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks
You're welcome Praveen! Glad I was able to help.

Your second question is also answered, try and close that as well.