1、在XLL中,把函數定義成不同的類型,在Excel中的實際效果也不同,具體如下:
pxMacroType value 0 1 2
Can be called from a worksheet Yes Yes No
Can be called from a macro sheet Yes Yes Yes
Can be called from a defined name definition Yes Yes Yes
Can be called from a conditional format expression Yes Yes No
Listed in the Function Wizard for worksheet functions No Yes No
Listed in the Function Wizard for macro sheet functions No Yes Yes
注冊函數需要使用Excel4的xlfRegister參數,其參數說明如下:
pxModuleText XLL名字,可以通過xlGetName獲取
pxProcedure 推薦使用字符串,表示在XLL 代碼中的函數名
pxTypeText 表示函數參數的類型和函數返回值的類型,字符串
pxFunctionText 函數名,在Excel中顯示的名稱
pxArgumentText 參數描述字符串
pxMacroType 函數類型: 0,1,2
pxCategory 函數類別
pxShortcutText 命令(類型2)的快捷鍵,"A" assigns this command to CONTROL+SHIFT+A
pxHelpTopic 幫助的引用字符串
pxFunctionHelp 函數向導中的描述
pxArgumentHelp1 函數參數1描述,后面依次類推
為了直觀看到不同函數類型的效果,我用ExcelDna做了個演示文件,在文件中實現了XLL的加載、卸載;函數的注冊、反注冊等功能。
由於ExcelDna的限制,直接使用xlfRegister更改函數類型,會造成返回值的錯誤。因此,例子中的MySub函數返回值無意義。


工作表函數向導 宏表函數向導
2、XLL可以通過另一個XLL進行加載和卸載,在Excel開發里面,這是最方便的升級方式。把俺的DNATool.XLL放到同一目錄下,通過加載項里面的菜單可以加載。注意名字必須完全一致,俺的代碼只認識這個名字 :)

文件下載 TestXllFunction
有什么建議歡迎聯系QQ:564955427
************************************************
Conditional Formatting Formulas Must Have References - If you have a conditional formatting formula that references a UDF defined in another workbook such as an add-in, then you must add a Reference to that workbook before you can reliably use the functions defined in it. If you don't, you will (sometimes) get the error 'This type of reference cannot be used in a Conditional Formatting Formula'. To add a reference, press Alt+F11 to go to the code editor, then go to Tools, References, and add the reference to this workbook by project name for the project containing the VBA code you need.
條件格式里面只能使用工作簿本身的公式,或者在VBE中引用的xla、xlsm等文件里面的公式。加載宏即使已經加載,但沒有引用的話,依然不能在條件格式的公式里面使用。
************************************************
http://msdn.microsoft.com/en-us/library/bb687858.aspx Accessing XLL Code in Excel
Calling XLL Functions Directly from Excel
Once they are registered, XLL worksheet and macro sheet functions can be called from anywhere a built-in function can be called from:
A single-cell or array formula on a worksheet.
A single-cell or array formula on a macro sheet.
The definition of a defined name.
The condition and limit fields in a conditional format dialog box.
From another add-in via the C API function xlUDF.
From Visual Basic for Applications (VBA) via the Application.Run method.
You can obtain a reference to the calling cell or range of cells within your function using the C API function xlfCaller. If the function was called from the cell’s conditional format expression, you are still returned a reference to the associated cell or cells, so you cannot assume that the cell’s formula contains the XLL function. If your function was called from a VBA user-defined function (UDF), xlfCaller again returns the address of the cells that called the VBA function. For more information, see xlfCaller.
Registering Functions and Commands with Excel
Registration tells Excel the following about a DLL entry point:
Whether it is hidden or, if a function, whether it is visible in the Function Wizard.
Whether it is callable only from an XLM macro sheet, or also from a worksheet.
If a command, whether it is a worksheet function or a macro sheet equivalent function.
What its XLL/DLL export name is, and what name you want Excel to use.
If it is a function:
What data types it returns and takes as arguments.
Whether it returns its result by modifying an argument in place.
Whether it is volatile.
Whether it is thread safe (supported starting in Excel 2007).
What text the Paste Function Wizard and AutoComplete editor should display to help with calling the function.
Which function category it should be listed under.
This is all achieved using the C API function xlfRegister, equivalent to the XLM function REGISTER.
