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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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
[TABLE="width: 384"]
<colgroup><col width="64" span="6" style="width:48pt"> </colgroup><tbody>[TR]
[TD="class: xl69, width: 64"]No_1[/TD]
[TD="class: xl70, width: 64"]2[/TD]
[TD="class: xl70, width: 64"]1[/TD]
[TD="class: xl70, width: 64"] [/TD]
[TD="class: xl70, width: 64"] [/TD]
[TD="class: xl71, width: 64"]1.5[/TD]
[/TR]
[TR]
[TD="class: xl64, width: 64"]2[/TD]
[TD="class: xl63, width: 64"]2[/TD]
[TD="class: xl63, width: 64"]0[/TD]
[TD="class: xl63, width: 64"] [/TD]
[TD="class: xl63, width: 64"] [/TD]
[TD="class: xl65, width: 64"]2[/TD]
[/TR]
[TR]
[TD="class: xl64, width: 64"]3[/TD]
[TD="class: xl63, width: 64"] [/TD]
[TD="class: xl63, width: 64"]2[/TD]
[TD="class: xl63, width: 64"] [/TD]
[TD="class: xl63, width: 64"] [/TD]
[TD="class: xl65, width: 64"]2[/TD]
[/TR]
[TR]
[TD="class: xl66, width: 64"]4[/TD]
[TD="class: xl72, width: 64"]*[/TD]
[TD="class: xl67, width: 64"]3[/TD]
[TD="class: xl67, width: 64"] [/TD]
[TD="class: xl67, width: 64"] [/TD]
[TD="class: xl68, width: 64"]3[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Hi, Oh ok, Thank you. That is possible If I have a numbers in next columns. If I have for example:





[TABLE="class: outer_border, width: 800, align: center"]
<tbody>[TR]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[TD="align: center"]E[/TD]
[TD="align: center"]F[/TD]
[TD="align: center"]G[/TD]
[TD="align: center"]h[/TD]
[TD="align: center"]i[/TD]
[TD="align: center"]j[/TD]
[/TR]
[TR]
[TD="align: center"]PRICE[/TD]
[TD="align: center"]%[/TD]
[TD="align: center"]PRICE[/TD]
[TD="align: center"]%[/TD]
[TD="align: center"]Diff[/TD]
[TD="align: center"]PRICE[/TD]
[TD="align: center"]%[/TD]
[TD="align: center"]Diff[/TD]
[TD="align: center"]...[/TD]
[TD]AVERAGE NO.[/TD]
[/TR]
[TR]
[TD="align: center"]1[/TD]
[TD="align: center"][/TD]
[TD="align: center"]2[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]4[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]N/A[/TD]
[TD="align: center"][/TD]
[TD="align: center"]0[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]2[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]1[/TD]
[TD="align: center"][/TD]
[TD="align: center"]**[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"]3[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

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
Hi, thank you. But I dont know How to use this .

If it is like this :

[TABLE="class: grid, width: 700"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[TD]F[/TD]
[/TR]
[TR]
[TD]No_1[/TD]
[TD]2[/TD]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[TD]=avg_ifs[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

The function dont work.


Use the below code for average if function......
 
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,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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