Average exclude text, 0, and blank cels - Function or macro

Marlowwe

New Member
Joined
Jan 12, 2016
Messages
46
Hi,

I would like to ask you for help with macro or custom function for this.

Calculate average for n cell and exclude 0, blank cells, -, text. Every time is different amount of calculated cells.

How to write macro or custom function.


My try is here but it is only for 2 calculating cell and Macro doesn´t work:

Code:
Sub Averageee()


Dim myValue As Range
Dim myValuee As Range


Set myValue = Application.InputBox("Cell No.1", Type:=8)
Set myValuee = Application.InputBox("Cell No.2", Type:=8)


ActiveCell.Formula = "=(SUM(SUMIF(" & myValue & ",<>0),SUMIF(" & myValuee & ",<>0)))/SUM(COUNTIF(" & myValue & ",>0),COUNTIF(" & myValuee & ",>0))"
ActiveCell.Select


End Sub

Thank you for your help.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi, why not just use AVERAGEIF() for example.


Excel 2013/2016
BCDEF
8211.5
9202
1022
11*33
Sheet1
Cell Formulas
RangeFormula
F8=AVERAGEIF(B8:E8,"<>0")
 
Upvote 0
Use the below code for average if function

Function avg_ifs()
Dim k As Long
For k = 2 To 4
Sheet2.Range("F" & k).Value = WorksheetFunction.AverageIfs(Sheet2.Range("B" & k & ":" & "E" & k), Sheet2.Range("B" & k & ":" & "E" & k), ">0")
Next
End Function

Data in sheet 2
No_121 1.5
220 2
3 2 2
4*3 3

<colgroup><col width="64" span="6" style="width:48pt"> </colgroup><tbody>
</tbody>
 
Upvote 0
Hi, Oh ok, Thank you. That is possible If I have a numbers in next columns. If I have for example:





ABCDEFGhij
PRICE%PRICE%DiffPRICE%Diff...AVERAGE NO.
124
N/A02
1**3

<tbody>
</tbody>

Excel 2013/2016[TR="class: outer_border, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]
Sheet1


Hi, why not just use AVERAGEIF() for example......
 
Upvote 0
I could be like this??????????

Code:
Sub AVERAGE_insertToActiveCell()
Dim myType As String
Dim Rng As Range
Dim Rngg As Range

Set Rng = Application.InputBox("Area for number calculation:", Type:=8)
Set Rngg = Application.InputBox("Area for name of column:", Type:=8)

myType = Application.InputBox("Name of column:", Type:=8)

'=AVERAGEIFS(Y15:AH15;$Y$9:$AH$9;"Price";Y15:AH15;"<>0")
ActiveCell.Formula = "=AVERAGEIFS(" & Rng.Address & "," & Rngg.Address & "," & myType & "," & Rng.Address & ",<>0)"
ActiveCell.Select


End Sub

But something is wrong and show error.


Try averageifS

=AVERAGEIFS(A2:I2,A$1:I$1,"Price",A2:I2,"<>0")
 
Upvote 0
Works for me..


Book1
ABCDEFGHIJ
1PRICE%PRICE%DiffPRICE%Diff...AVERAGE NO.
21242.333333333
3N/A022
41**32
Sheet1
Cell Formulas
RangeFormula
J2=AVERAGEIFS(A2:I2,A$1:I$1,"Price",A2:I2,"<>0")
J3=AVERAGEIFS(A3:I3,A$1:I$1,"Price",A3:I3,"<>0")
J4=AVERAGEIFS(A4:I4,A$1:I$1,"Price",A4:I4,"<>0")
 
Last edited:
Upvote 0
But something is wrong and show error.

It's not clear what ranges you are selecting with those inputboxes, but at a guess try amending this line:

Code:
ActiveCell.Formula = "=AVERAGEIFS(" & Rng.Address & "," & Rngg.Address & "," & myType & "," & Rng.Address & ",<>0)"

To:

Code:
ActiveCell.Formula = "=AVERAGEIFS(" & Rng.Address & "," & Rngg.Address & ",""" & myType & """," & Rng.Address & ",""<>0"")"
 
Upvote 0
Hey, Thanks a lot.

Here is my fina macro:

Code:
Sub AVERAGE_insertToActiveCell()
Dim myType As String
Dim Rng As Range
Dim Rngg As Range


Set Rng = Application.InputBox("Select area of numbers (in a row) for calculation:", Type:=8)
Set Rngg = Application.InputBox("Select Header area:" & vbCrLf & _
"The area must be the same as Selection of numebers from 1st step" & vbCrLf & _
"Select only one row, not merged cells", Type:=8)


myType = Application.InputBox("Select cell what column should be calculated:", Type:=8)


'ActiveCell.Formula = "=subtotal(" & myType & "," & Rng.Address & ")"
'=AVERAGEIFS(Y15:AH15;$Y$9:$AH$9;"Price";Y15:AH15;"<>0")
ActiveCell.Formula = "=AVERAGEIFS(" & Rng.Address & "," & Rngg.Address & ",""" & myType & """," & Rng.Address & ",""<>0"")"
ActiveCell.Select


End Sub

It's not clear what ranges you are selecting with those inputboxes, but at a guess try amending this line:............
 
Upvote 0

Forum statistics

Threads
1,215,004
Messages
6,122,656
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top