Description
Let's make an Excel-specific inspection that flags instructions that can't be used in a UDF, causing undesired behavior. Inspection should ignore modules with Option Private Module
set, and only look at standard/procedural modules.
ActiveCell
calls that mean to work against the cell being calculated, should be replaced withApplication.Caller
.Application.Calculation
,Application.EnableEvents
,Application.ScreenUpdating
assignments, are illegal.Range.Activate
calls can't be removed without breaking the code, but need to be flagged as illegal anyway.Range.Insert
,Range.Delete
,Range.BorderAround
, assignment to anyFormatCondition
member (or anyFormatConditions
method call), assignments to anyBorder
members, or anyFont
members, are illegal.- Assignments to
Range.Value
,Range.Value2
, andRange.[_Default]
(or to anyRange
property for that matter) are illegal and should be removed. Names.Add
calls are illegal and should be removed.- Assignments to
Sheet.Name
(Worksheet.Name
),Sheets.Add
(Worksheets.Add
),Sheet.Delete
,Sheets.Delete
(Worksheet.Delete
,Worksheets.Delete
),Sheet.Copy
(Worksheet.Copy
), are all illegal too. Charts
method calls, andChart
property assignments and method calls are illegal.PivotTables
method calls, andPivotTable
property assignments and method calls are illegal.ListObject
,ListRow
andListColumn
method calls and property assignments are illegal.
Probably forgetting a number of others...
Operation is not supported in a User-Defined Function
A public function in a standard procedural module is exposed to Excel worksheets as a UDF. These functions can produce invalid results when invoked from a worksheet. If the function isn't intended to be exposed as a UDF, consider specifying 'Option Private Module'.
One quick-fix could be to move the function to another standard/procedural module with Option Private Module
specified; another option could be to specify Option Private Module
at the top of the module. Another quick-fix could be to outright remove the illegal statement. Support for "Fix in procedure" is a must here.