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

标签 vba excel user-defined-functions

我创建了以下函数:

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

请告诉我如何解决。

最佳答案

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

制作新工作簿

按 Alt+F11

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

将你的函数放在那里

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

现在您的函数将始终在每个工作簿中可用,而无需添加前缀

关于Excel 中的 VBA 公共(public)用户定义函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2620068/

相关文章:

excel - 当在一列中满足多个条件时,AverageIFs 多列

c++ - 如何跨多个函数使用变量?

sql - 在 Access/VBA 中构建 SQL 字符串

输入错误时,带有密码的 VBA 宏仍然可以运行

vba - 防止粘贴在多个范围内应用的数据验证(excel/VBA)

vba - 保护 Excel 工作簿(使用 VBA)免于与不同数据重复使用

excel - 在 VBA 中使用 IF

java - Excel VBA函数转Java poi

MYSQL,为日期|日期时间插入创建了用户定义的函数,但它不起作用

python - 如何使用自定义 udf 实现对列进行舍入