[英]EXCEL VBA Currency format in TextBox1
I want to format the value in TextBox1
with the following: 我想使用以下格式设置
TextBox1
的值:
TextBox1
even when user is not entering any value eg RM 0.00 TextBox1
,例如RM 0.00 TextBox1
, TextBox1
value will change from RM 0.00 to RM 12.34 User don't have to hit the ' .
TextBox1
输入1234时, TextBox1
值将从RM 0.00变为RM 12.34。用户不必按下' .
', the integer will just move front eg RM 0.01 -> RM 0.12 -> RM 1.23 -> RM 12.34 as user press 1234 in the TextBox1
. TextBox1
按1234时,RM 0.01-> RM 0.12-> RM 1.23-> RM 12.34。 If any misunderstanding elsewhere within my questions, please let me know and I will correct it. 如果我的问题中的其他地方有任何误解,请让我知道,我将予以纠正。 Thank you all in advance.
谢谢大家。
First: Your number format is easy enough. 第一:您的数字格式非常简单。 Open the Number format dialog box and create a custom format using this string:
打开数字格式对话框,并使用以下字符串创建自定义格式:
"RM"#,#0.00 “ RM”#,#0.00
Second: To show empty cells as a zero, you have two options. 第二:要将空单元格显示为零,您有两个选择。
Third (and finally): As far as the automatic placement of decimals, this is not uncommon. 第三(也是最后一个):就自动放置小数而言,这种情况并不罕见。 Simply follow the steps in this tutorial:
只需按照本教程中的步骤操作:
How to insert decimal points automatically in Excel 如何在Excel中自动插入小数点
I hope this helps, good luck! 希望对您有帮助,祝您好运!
EDIT: 编辑:
At first glance I thought you needed help with cell formatting, but you need help with a textbox control within a VBA Userform and/or a the worksheet itself. 乍一看,我认为您需要有关单元格格式的帮助,但是您需要有关VBA用户窗体和/或工作表本身中的文本框控件的帮助。 The tool that you need to accomplish this task is an Input Mask.
完成此任务所需的工具是输入掩码。 However, from what I understand, there is currently no built-in support for this in a VBA userform.
但是,据我了解,目前VBA用户窗体中对此没有内置支持。 You can build code to do this when the user changes or exits the box.
您可以构建代码以在用户更改或退出框时执行此操作。
I would recommend code when they exit that will first validate the input to confirm it is numeric, then change the value to match the desired format. 我建议在退出时首先验证输入以确认其为数字的代码,然后更改值以匹配所需的格式。 Here is an example:
这是一个例子:
Private Sub myTextbox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim myVal As String
myVal = Trim(myTextbox.Value)
If myVal = "" Then Exit Sub 'Ignore this subroutine if the box is empty.
If IsNumeric(myVal) = False Then 'If the input is not a number, ask for correct input.
MsgBox "Please enter a number value."
myTextbox.SetFocus
Exit Sub
ElseIf InStr(1, myVal, ".", vbTextCompare) > 0 Then 'If the input includes decimal, use it.
myTextbox.Value = "RM " & Format(myVal, "#,##0.00")
ElseIf InStr(1, myVal, ".", vbTextCompare) = 0 Then 'If the input doesn't include decimal, enter one and divide by 100.
myTextbox.Value = "RM " & (Format(myVal, "#,##0.00") / 100)
End If
End Sub
And if you want to get really fancy, you can first search the input for "RM". 如果您真的想花哨的话,可以先在输入中搜索“ RM”。 This would ensure that an "invalid input" message box wouldn't pop up if the user entered a value such as "RM 123.45".
这将确保如果用户输入诸如“ RM 123.45”的值,则不会弹出“无效输入”消息框。 Unfortunately I don't have time to write that code out for you at the moment.
不幸的是,目前我没有时间为您编写该代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.