将函数填充到VBA中的货币值

问题描述 投票:0回答:1

我试图在VBA中制作类似str_pad函数的东西,返回领先的零货币值。

例如,如果我想用6位数填充它:

Input:
$ 423,67
Output:
000423

到目前为止,它可以添加那些前导零,但是当它没有小数时我无法找到实现它的方法:

Input:
$ 423,00
Current output:
000423
Desired output:
042300

由于用户通常在逗号后面不包含零,因此我的代码应该能够将它们放在输出上。

码:

Function str_pad(text As Variant, totalLength As Integer, padCharacter As String) As String
    If totalLength > Len(CStr(text)) Then
        'str_pad = String(totalLength - Len(CStr(text)), padCharacter) & CStr(text)
    Else
        str_pad = text
    End If
End Function
excel vba excel-vba
1个回答
0
投票

如何将你的函数更改为如下所示,这将根据需要格式化数字,然后删除小数分隔符,所以数字显示为预期,我省略了totalLength,因为这将填充6位数字:

Function str_pad(text As Variant, padCharacter As String) As String
        str_pad = Format(text, "0000.00")
        str_pad = Replace(str_pad, ".", "")
End Function

要使用此功能,您可以使用如下公式:=str_pad(A1,0)

© www.soinside.com 2019 - 2024. All rights reserved.