显示货币的 VBA 文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5980349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:29:23  来源:igfitidea点击:

VBA Text Box displaying Currency

excelvalidationvbauserform

提问by bdubb

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:

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

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

Also seen some with cDbl

也看到了一些与 cDbl

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

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

回答by chris neilsen

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 = "
Private Sub TextBox1_Change()    
  If TextBox1 = vbNullString Then Exit Sub    
  If IsNumeric(TextBox1) Then CurrencyTransform(TextBox1)
End Sub
.00" TextBox1 = "
Private sub textbox1_AfterUpdate()
    textbox1 = format(textbox1, "$#,##0.00")
end sub
.00" End Sub

回答by Lance Roberts

You need to use the TextBox Change event, like:

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

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

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

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

回答by Eduardo Guarana

Try simply this...

试试这个...

##代码##

回答by Jacob

Try this:

尝试这个:

##代码##

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 .valueafter 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 .valueand got weird results. Instead of, for example, $100 for TextBox1.Value + TextBox2.Valuewhere TextBox1.Valueis equal to $25 and TextBox2.Valueis equal to $75, I would get "$25$75".

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