显示货币的 VBA 文本框
我有一个带有多个文本框的表单,用于用户输入(这是在用户表单中,而不是在电子表格上)。我有几个与货币相关的框,当用户在框中输入他们的条件时,我需要它们显示逗号和小数点。到目前为止,我在网上找到了一堆相同的公式,但是当我在框中输入我的数字时,它会显示 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当用户输入数据时进行格式化变得非常棘手。输入完成后格式化可能会更好。
如果条目被视为无效,还可以验证条目并恢复旧值
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
您需要使用 TextBox Change 事件,例如:
然后您创建CurrencyTransform 函数来修改它在 TextBox 中显示的内容。
You need to use the TextBox Change event, like:
You then create the CurrencyTransform function to modify what it shows in the TextBox.
简单地尝试一下这个...
Try simply this...
我写这篇文章的灵感来自克里斯的解决方案。它在用户打字时起作用!
I wrote this inspired by chris' solution. It works while user is typing!
试试这个:
这对我来说效果很好,所以它也应该对你有帮助。
如果要进行涉及多个文本框的计算,请勿在文本框名称后使用
.value
。相反,在文本框的名称之前使用val(
,同时在其后面加上结束括号。我使用.value
并得到了奇怪的结果。而不是,例如 $100对于TextBox1.Value + TextBox2.Value
,其中TextBox1.Value
等于 $25,TextBox2.Value
等于 $75,我会得到“ 25 美元 75 美元”。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
.value
after the name of the text box. Instead, useval(
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 forTextBox1.Value + TextBox2.Value
whereTextBox1.Value
is equal to $25 andTextBox2.Value
is equal to $75, I would get "$25$75".