简体   繁体   English

TextBox1中的EXCEL VBA货币格式

[英]EXCEL VBA Currency format in TextBox1

I want to format the value in TextBox1 with the following: 我想使用以下格式设置TextBox1的值:

  1. Ringgit Malaysia currency as in (RM) without the bracket 不含括号的马来西亚林吉特货币(RM)
  2. make it 2 decimal place eg RM 10.00 将其设置为小数点后两位,例如RM 10.00
  3. make the format appear in TextBox1 even when user is not entering any value eg RM 0.00 即使用户未输入任何值,也使格式显示在TextBox1 ,例如RM 0.00
  4. allow user to enter integer only and also eg when user type 1234 in 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. 第二:要将空单元格显示为零,您有两个选择。

  1. If the cells is a result of an equation or data somewhere else, you can use a formula; 如果单元格是方程式或其他地方的数据的结果,则可以使用公式;否则,可以使用公式。 eg, =if(A1="",0,A1) This will take the value of A1 unless it is empty, in which case it would be 0. 例如= if(A1 =“”,0,A1)除非A1为空,否则它将采用A1的值,在这种情况下它将为0。
  2. You may also set the worksheet to display all zero values under options in the file tab. 您还可以将工作表设置为在文件选项卡的选项下显示所有零值。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM