【问题标题】:VBA Public User Defined Function in ExcelExcel中的VBA公共用户定义函数
【发布时间】:2011-02-06 21:12:35
【问题描述】:

我已经创建了下面的函数:

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

请告诉我如何解决。

【问题讨论】:

    标签: vba excel user-defined-functions


    【解决方案1】:

    此类功能的最佳位置是在插件中... 制作插件:

    制作一个新的工作簿

    按 alt+F11

    创建一个模块,将其命名为 MyFunctions 或其他有意义的东西

    把你的功能放在那里

    完成所有这些后,将工作簿另存为 ExcelAddin (.xlam) 并关闭它。 转到 Excel 选项(或工具/插件)并选择您的插件(或转到插件选项卡并单击转到然后为 excel 07 找到它)

    现在您的功能将始终在每个工作簿中可用,而无需为其添加前缀

    【讨论】:

    • 如何命名一个模块?当我创建它时,它只是命名为“模块”,当我 它时,上下文菜单没有为我提供“重命名”选项。
    • @Alexis,当您选择模块名称时,下方的窗口部分将允许您编辑模块属性,包括名称。单击并编辑名称并按回车键,它将被重命名。
    【解决方案2】:

    如果您的 UDF 位于您调用的工作簿以外的工作簿中,请在 udf 前加上工作簿名称。例如

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

    【讨论】:

    • 不确定我会使用这种方法.. 但是 +1 以获得晦涩的 UDF 知识迪克!
    【解决方案3】:

    查看这个相关问题:Create a custom worksheet function in Excel VBA

    总结:
    你所拥有的应该可以工作。
    根据该问题的 cmets,您应该将您的用户定义函数放在 ThisWorkbook 以外的任何模块中。

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        检查错字:函数是fyi 而不是fyis

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

        【讨论】:

        • fyis 也是问题代码中的一个函数。
        猜你喜欢
        • 1970-01-01
        • 2015-08-12
        • 2018-06-06
        • 1970-01-01
        • 2019-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多