Excel中的VBA公共用户定义函数

我已经创build了以下function:

Option Explicit Public Function fyi(x As Double, f As String) As String Application.Volatile Dim data As Double Dim post(5) post(1) = "Ribu " post(2) = "Juta " post(3) = "Milyar " post(4) = "Trilyun " post(5) = "Ribu Trilyun " Dim part As String Dim text As String Dim cond As Boolean Dim i As Integer If (x < 0) Then fyi = " " Exit Function End If If (x = 0) Then fyi = "Nol" Exit Function End If If (x < 2000) Then cond = True End If text = " " If (x >= 1E+15) Then fyi = "Nilai Terlalu Besar" Exit Function End If For i = 4 To 1 Step -1 data = Int(x / (10 ^ (3 * i))) If (data > 0) Then part = fyis(data, cond) text = text & part & post(i) End If x = x - data * (10 ^ (3 * i)) Next text = text & fyis(x, False) fyi = text & f End Function Function fyis(ByVal y As Double, ByVal conds As Boolean) As String Dim datas As Double Dim posts(2) posts(1) = "Puluh" posts(2) = "Ratus" Dim parts As String Dim texts As String 'Dim conds As Boolean Dim j As Integer Dim value(9) value(1) = "Se" value(2) = "Dua " value(3) = "Tiga " value(4) = "Empat " value(5) = "Lima " value(6) = "Enam " value(7) = "Tujuh " value(8) = "Delapan " value(9) = "Sembilan " texts = " " For j = 2 To 1 Step -1 datas = Int(y / 10 ^ j) If (datas > 0) Then parts = value(datas) If (j = 1 And datas = 1) Then y = y - datas * 10 ^ j If (y >= 1) Then posts(j) = "belas" Else value(y) = "Se" End If texts = texts & value(y) & posts(j) fyis = texts Exit Function Else texts = texts & parts & posts(j) End If End If y = y - datas * 10 ^ j Next If (conds = False) Then value(1) = "Satu " End If texts = texts & value(y) fyis = texts End Function 

当我返回到Excel并在单元格中键入=fyi(500,"USD")时,它将返回#name

请告诉我如何解决。

像这样的function最好的地方是在一个插件…做一个插件:

制作一个新的工作簿

击中ALT + F11

创build一个模块,称之为MyFunctions或其他有意义的东西

放下你的function

完成所有这些后,将工作簿保存为ExcelAddin(.xlam)并closures它。 转到Excel选项(或工具/插件),并select你的插件(或去插件选项卡,然后单击开始,然后find它为Excel 07)

现在,您的function将始终在每个工作簿中都可用,而无需将其前缀

看到这个相关的问题: 在Excel VBA中创build一个自定义工作表函数

综上所述:
你有什么应该工作。
基于对该问题的评论,您应该将您的用户定义的函数放在ThisWorkbook以外的任何模块中

如果您的UDF位于工作簿以外的工作簿中,请以工作簿名称作为前缀。 例如

 =PERSONAL.XLS!fyi(500,"USD") 

确保你的函数在模块中,而不在工作表中。

检查错字:function不是fyis

看最后一行fyis = texts ,应该是fyi = texts