Microsoft Office Excel

From miki
Revision as of 09:32, 14 November 2013 by Mip (talk | contribs) (Created page with '== User Defined Functions in VBA == * Reference: [http://www.cpearson.com/excel/writingfunctionsinvba.aspx] ;Example of function: <source lang=vb> Function RectangleArea(Height …')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

User Defined Functions in VBA

Example of function
Function RectangleArea(Height As Double, Width As Double) As Double
    RectangleArea = Height * Width
End Function
Where to put the code
  • The UDF must be placed in a standard code module (not in one of the Sheet modules and not in the ThisWorkbook module).
  • To create a standard module: go to Insert menu, and choose Module.
  • Rename the module by pressing F4 and display the Properties, and change the Name property.
Calling the UDF from the same workbook
  • Simply use the function name. For instance, type in cell A3:
=RectangleArea(A1,A2)