显示货币的 VBA 文本框

发布于 2024-11-07 04:09:50 字数 312 浏览 3 评论 0原文

我有一个带有多个文本框的表单,用于用户输入(这是在用户表单中,而不是在电子表格上)。我有几个与货币相关的框,当用户在框中输入他们的条件时,我需要它们显示逗号和小数点。到目前为止,我在网上找到了一堆相同的公式,但是当我在框中输入我的数字时,它会显示 4.00(如果我先按 4),之后我所能更改的就是第二个 0。这是我看到的类似内容在线:

textbox1 = format(textbox1, "$#,##0.00")

还看到了一些 cDbl

无论我尝试什么,它都不会让我输入超过我输入的第一个数字的任何内容。我需要帮助。谢谢!

I have a form with a number of text boxes for user input (this is in a User Form not on the spreadsheet). I have a few boxes that are related to currency and I need them to show the comma and decimal point as the user enters their criteria into the box. So far I have found a bunch of the same formulas online but when I input my number into the box it goes with 4.00 (if i hit 4 first) and all i can change after that is the second 0. Here is something similar I see online:

textbox1 = format(textbox1, "$#,##0.00")

Also seen some with cDbl

No matter what I try it won't let me enter anything more than the first number I enter. I need help. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

热风软妹 2024-11-14 04:09:50

当用户输入数据时进行格式化变得非常棘手。输入完成后格式化可能会更好。
如果条目被视为无效,还可以验证条目并恢复旧值

Dim TextBox1oldValue As String

Private Sub TextBox1_AfterUpdate()
    If IsNumeric(TextBox1) Then
        TextBox1 = Format(TextBox1, "$#,##0.00")
    Else
        TextBox1 = TextBox1oldValue
    End If
End Sub

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    If IsNumeric(TextBox1) Then
        TextBox1oldValue = Format(TextBox1, "$#,##0.00")
    End If
End Sub

Private Sub UserForm_Initialize()
    TextBox1oldValue = "$0.00"
    TextBox1 = "$0.00"
End Sub

Formatting as the user types in data gets very tricky. May be better to format after the entry is complete.
Entry can also be validated and old value restored if entry deemed invalid

Dim TextBox1oldValue As String

Private Sub TextBox1_AfterUpdate()
    If IsNumeric(TextBox1) Then
        TextBox1 = Format(TextBox1, "$#,##0.00")
    Else
        TextBox1 = TextBox1oldValue
    End If
End Sub

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    If IsNumeric(TextBox1) Then
        TextBox1oldValue = Format(TextBox1, "$#,##0.00")
    End If
End Sub

Private Sub UserForm_Initialize()
    TextBox1oldValue = "$0.00"
    TextBox1 = "$0.00"
End Sub
柳若烟 2024-11-14 04:09:50

您需要使用 TextBox Change 事件,例如:

Private Sub TextBox1_Change()    
  If TextBox1 = vbNullString Then Exit Sub    
  If IsNumeric(TextBox1) Then CurrencyTransform(TextBox1)
End Sub

然后您创建CurrencyTransform 函数来修改它在 TextBox 中显示的内容。

You need to use the TextBox Change event, like:

Private Sub TextBox1_Change()    
  If TextBox1 = vbNullString Then Exit Sub    
  If IsNumeric(TextBox1) Then CurrencyTransform(TextBox1)
End Sub

You then create the CurrencyTransform function to modify what it shows in the TextBox.

不知在何时 2024-11-14 04:09:50

简单地尝试一下这个...

Private sub textbox1_AfterUpdate()
    textbox1 = format(textbox1, "$#,##0.00")
end sub

Try simply this...

Private sub textbox1_AfterUpdate()
    textbox1 = format(textbox1, "$#,##0.00")
end sub
千仐 2024-11-14 04:09:50

我写这篇文章的灵感来自克里斯的解决方案。它在用户打字时起作用!

Private waiting As Boolean

Private Sub TextBox1_Change()
    If waiting Then Exit Sub
    
    waiting = True
    TextBox1 = formatAsCurrency(TextBox1)
    waiting = False
End Sub

Private Function formatAsCurrency(v As String)
    If v = "" Then
        formatAsCurrency = Format(0, "0.00")
    Else
        Dim vv As Variant
        vv = Replace(v, ",", "")
        vv = Replace(vv, ".", "")
        formatAsCurrency = Format(vv / 100, "#,##0.00")
    End If
End Function

I wrote this inspired by chris' solution. It works while user is typing!

Private waiting As Boolean

Private Sub TextBox1_Change()
    If waiting Then Exit Sub
    
    waiting = True
    TextBox1 = formatAsCurrency(TextBox1)
    waiting = False
End Sub

Private Function formatAsCurrency(v As String)
    If v = "" Then
        formatAsCurrency = Format(0, "0.00")
    Else
        Dim vv As Variant
        vv = Replace(v, ",", "")
        vv = Replace(vv, ".", "")
        formatAsCurrency = Format(vv / 100, "#,##0.00")
    End If
End Function
你的背包 2024-11-14 04:09:50

试试这个:

Private Sub TextBox1_Change()
    TextBox1.Value = Format(TextBox1.Value, "$#,##0.00")
End Sub

这对我来说效果很好,所以它也应该对你有帮助。

如果要进行涉及多个文本框的计算,请勿在文本框名称后使用 .value。相反,在文本框的名称之前使用 val( ,同时在其后面加上结束括号。我使用 .value 并得到了奇怪的结果。而不是,例如 $100对于 TextBox1.Value + TextBox2.Value,其中 TextBox1.Value 等于 $25,TextBox2.Value 等于 $75,我会得到“ 25 美元 75 美元”。

Try this:

Private Sub TextBox1_Change()
    TextBox1.Value = Format(TextBox1.Value, "$#,##0.00")
End Sub

This worked for me just fine, so it should help you as well.

If you want to do calculations that involve multiple text boxes, don't use .value after the name of the text box. Instead, use val( before the name of the text box while following it with an end parenthesis. I used .value and got weird results. Instead of, for example, $100 for TextBox1.Value + TextBox2.Value where TextBox1.Valueis equal to $25 and TextBox2.Value is equal to $75, I would get "$25$75".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文