Hans Hamm said:
I am getting a different error message!
"Run-time error '1004": Unable to get the AverageIfs
property of the WorksheetFunction class"
I don't recall if you ever told us what version of Excel you are using.
AverageIfs is valid only in Excel 2007 and later.
If you are using Excel 2007 and later, I believe that misleading runtime
error can actually mean that are no cells in Arng that meets both
conditions. In other words, the runtime error is the same as the #DIV/0
error we would get in Excel.
I suggest that you try using AVERAGEIFS in an Excel formula with the same
ranges to see if you do, in fact, get a #DIV/0 error. Note: This is just
for debugging purposes.
If you might expect #DIV/0 errors (i.e. AVERAGEIFS of a null range), you can
avoid the runtime error in one of two ways.
On Error Resume Next
Range("a1") = WorksheetFunction.AverageIfs(...)
If Err > 0 Then Range("a1") = 0 ' this might be redundant
On Error GoTo 0
or
On Error Resume Next
Range("a1") = Application.AverageIfs(...)
If Err > 0 Then Range("a1") = 0 ' optional
On Error GoTo 0
In the first case, VBA might store a zero into Range("a1") automagically.
In the second case, VBA will store a #DIV/0 (?) error into Range("a1").
That is the difference between Application.AverageIfs and
(Application.)WorksheetFunction.AverageIfs: Application.AverageIfs returns
"soft" errors in the form of Excel error codes, e.g. xlErrDiv0.
However, IIRC, Application.AverageIfs is deprecated. That is, it is a
legacy feature, and it might be obsoleted in the future. So arguably,
WorksheetFunction.AverageIfs is better to use.
Hans Hamm said:
Set Arng = Sheet2.Range("E2:E" & Sheet2.Range("AJ" &
Rows.Count).End(xlUp).Row)
Set Srng = Sheet1.Range("C1")
Set Erng = Sheet1.Range("E1") [....]
x = Application.WorksheetFunction.AverageIfs(Arng, Arng, ">=" & Srng, "<="
& Erng)
I assume that Sheet1 and Sheet2 are valid VBA __objects__. If they weren't,
I would expect a runtime error before the AverageIfs usage.
If Sheet1 and Sheet2 are valid VBA objects, there is __nothing__ wrong
syntactically with Sheet1.Range and Sheet2.Range.
Howevever, the __object__ names are not necessarily the same as __sheet__
names. So the __objects__ Sheet1 and Sheet2 might not be the intended
worksheets.
You might have intended to write Sheets("Sheet1").Range and
Sheets("Sheet2").Range instead.
Look carefully at the information in the Project Explorer. If I have time
later, I will provide an image to explain this better.
Hans Hamm said:
One thing I did not get in your reply was the "with"
is not needed. As I understand what I have OArng is
where I want the average to be reported (put the average
here on Sheet1-F3).
The With construct has nothing to do with where a value is assigned.
The With construct is only a syntax shorthand. For example:
With Range("a1")
.Value = 123
.HorizontalZAlignment = xlLeft
End With
is a shorthand for Range("a1").Value. Use the With construct when you want
to access multiple properties of an object. But IMHO, we should use the
With construct sparingly because it is error-prone: note the need to put a
period (.) before each property.
If you want to assign the AverageIfs result to OArng, use the following
without the With construct:
OArng = Application.WorksheetFunction.AverageIfs(Arng, Arng, ">=" & Srng,
"<=" & Erng)