2010-04-12 80 views
3

我已經創建了下面的功能:VBA公共用戶定義函數在Excel

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

請告訴我如何解決。

回答

3

確保您的功能位於模塊中,而不是工作表中。

6

如果您的UDF所在的工作簿不是您打電話給的工作簿,請以工作簿名稱作爲前綴。例如。

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

不知道我會使用這種方法..但+1爲晦澀的UDF知識迪克! – 2010-04-13 09:47:05

+1

同意MarkNold。良好的工作@Dick Kusleika – hnk 2014-07-03 02:47:35

11

的功能,如這一點,最好的地方是在一個插件... 要使一個插件:

做一個新的工作簿

按下Alt + F11

創建模塊,稱之爲MyFunctions或其他東西有意義的

把你的功能放在那裏

完成所有這些後,將工作簿保存爲ExcelAddin(.xlam)並關閉它。 轉到Excel選項(或工具/插件)並選擇你的插件(或去插件選項卡,然後單擊轉到然後找到它爲Excel 07)

現在你的功能將永遠在每個工作簿可用,而無需前綴它

+0

你如何命名模塊?當我創建它時,它被命名爲「模塊」,當我在其上時,上下文菜單不提供「重命名」選項。 – Alexis 2017-03-16 00:56:13

-2

檢查錯字:功能是fyi而不是fyis

看到最後一行fyis = texts,應該是fyi = texts

+0

'fyis'也是代碼中的一個函數。 – 2015-11-22 14:07:35

相關問題