TextBox1中的EXCEL VBA货币格式
我想用以下内容格式化TextBox1中的EXCEL VBA货币格式,excel,vba,format,currency,Excel,Vba,Format,Currency,我想用以下内容格式化TextBox1中的值: 马来西亚林吉特货币(RM),不带括号 小数点后2位,例如10.00 RM 即使用户未输入任何值,例如RM 0.00,也要使格式显示在TextBox1中 仅允许用户输入整数,例如,当用户在TextBox1中键入1234时,TextBox1值将从RM 0.00更改为RM 12.34。用户不必点击“”,当用户在TextBox1中按1234时,整数将向前移动,例如RM 0.01->RM 0.12->RM 1.23->RM 12.34 如果我的问题中有任何误解
TextBox1
中的值:
TextBox1
中TextBox1
中键入1234时,TextBox1
值将从RM 0.00更改为RM 12.34。用户不必点击“
”,当用户在TextBox1
中按1234时,整数将向前移动,例如RM 0.01->RM 0.12->RM 1.23->RM 12.34如果我的问题中有任何误解,请让我知道,我会纠正它。提前谢谢大家。首先:您的号码格式很简单。打开“数字格式”对话框,并使用以下字符串创建自定义格式: “RM”#,#0.00 秒:要将空单元格显示为零,有两个选项
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
如果你真的想变得有趣,你可以先在输入中搜索“RM”。这将确保在用户输入诸如“RM 123.45”之类的值时不会弹出“无效输入”消息框。不幸的是,我现在没有时间为您写出代码。我想直接在文本框中格式化,而不是在单元格中。对不起,如果这让你感到困惑,我想知道这是否可能。谢谢你的努力,非常感谢!