Excel VBA unable to get the AverageIf, Run-time error 1004

EuginG

New Member
Joined
May 3, 2016
Messages
14
Office Version
  1. 2016
Platform
  1. Windows
Hi!

Please help me to solve a problem.

I have Excel’s reports created by some analytic software. There are many data there.
One of my actions is to apply AverageIf function for two columns (average from L column based on G column criteria).
Moreover L column numbers originally go as a text (e.g. 15). And I need to convert it to countable numbers first (e.g. 15,00).

It is Ok! Looks like I’ve found a solution for conversion.

But the problem is that if the Excel’s report does not have at least one of seven values (in G column)that I am searching for, script goes to Error (Debug) and do not work properly. And I need to delete manually the strings which contain unnecessary values. Then Run script again. Because report’s data may vary (hundreds of rows), it becomes a problem to my script.

How can I avoid the Error and force strings to skipping, if they are unnecessary?

Sub test()

Dim Cell As Range, gRange As Range, lRange As Range

Set gRange = Range("G4:G1000")
Set lRange = Range("L4:L1000")

lRange.Select

With Selection
Selection.NumberFormat = "0.00"
.Value = .Value
End With

Range("M2:T2").EntireColumn.NumberFormat = "#.00"

Range("M2").Value = Application.WorksheetFunction.AverageIf(gRange, 2, lRange)
Range("O2").Value = Application.WorksheetFunction.AverageIf(gRange, 3, lRange)
Range("N2").Value = Application.WorksheetFunction.AverageIf(gRange, 2.1, lRange)
Range("P2").Value = Application.WorksheetFunction.AverageIf(gRange, 11, lRange)
Range("R2").Value = Application.WorksheetFunction.AverageIf(gRange, 19, lRange)
Range("S2").Value = Application.WorksheetFunction.AverageIf(gRange, 21, lRange)
Range("T2").Value = Application.WorksheetFunction.AverageIf(gRange, 33, lRange)


End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Use the AverageIf method of the Application object instead. This way the error won't cause your code to break, and a #DIV/0! error will be entered in the cell...

Code:
Range("M2").Value = Application.AverageIf(gRange, 2, lRange)

Hope this helps!
 
Upvote 0
Use the AverageIf method of the Application object instead. This way the error won't cause your code to break, and a #DIV/0! error will be entered in the cell...

Code:
Range("M2").Value = Application.AverageIf(gRange, 2, lRange)

Hope this helps!

It helped! Thank you, Domenic! So simple.. :)
 
Upvote 0

Forum statistics

Threads
1,215,972
Messages
6,128,016
Members
449,414
Latest member
sameri

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