Coding best practices for calling worksheet functions which may return an error?

SliderJeff

Board Regular
Joined
Oct 5, 2016
Messages
63
Hey all,

So I just stumbled across an issue which I hadn't expected when processing certain datafiles with my VBA using the WorksheetFunction.AverageIf(). Evidently it's possible for me to process an array with this function call which may actually contain ONLY zeros, and hence throw an error since there is no non-zero data to process.

Here's an example:

Code:
    outputWorkbook.Worksheets("Sheet1").Cells(fileNum + wbScript.GetConstant("FILE_OFFSET"), wbScript.GetConstant("CURRENT_COL")).Value = Application.WorksheetFunction.AverageIf(inputWorkbook.Worksheets(fileName).Columns("B:B"), ">0")

Since the function was trying to average only values that are ">0", and the values in Column B were all 0, I got an error and the script stopped.

For brevity, I'll abbreviate the left side of the equation above as "output.Value".

If such an issue occurs where there is an error thrown, I'd just like to do the following:

Code:
output.Value = 0

And then move on to the next line of code.

Given that I have upwards of 150 calls to worksheet functions like AverageIf, Index, Match, etc... I was wondering if I need to insert an:

Code:
On Error Resume Next

line of code before each and every single WorksheetFunction I call, or if there is a more universal/better way to code up error handling and still be able to do things like set the output value to a known value if there is an error?

Thanks for any help you can provide.

Regards,
Jeff
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
For the time being, I'm going to add code along these lines below everywhere I call a function.
Code:
    On Error Resume Next
    outputVal = Application.WorksheetFunction.Average(inputWorkbook.Worksheets(fileName).Columns("B:B"))
    On Error GoTo 0
    If IsEmpty(outputVal) Then
        outputVal = 0
    End If
    outputWorkbook.Worksheets("Sheet1").Cells(fileNum + wbScript.GetConstant("FILE_OFFSET"), wbScript.GetConstant("TEMPERATURE_COL")).Value = outputVal

Please let me know if there is a better way to handle this. If not, I hope this helps someone else with a similar question.

Regards,
Jeff
 
Upvote 0

Forum statistics

Threads
1,214,891
Messages
6,122,101
Members
449,066
Latest member
Andyg666

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