vba数字格式:更改多个activex文本框的格式



我有一堆activex文本框,我想做的是更改特定数量文本框的数字格式。

最终,我只想要一个子程序,我可以对我选择的所有文本框进行编码——文本框1、文本框2、文本框15——文本框的编号无关紧要,编号格式将为"###、###、##"。例如

Private sub textNumFormat_<whatever>() TextBox1.Text = Format(TextBox1.Text, "###,###,###") TextBox2.Text = Format(TextBox2.Text, "###,###,###") TextBox15.Text = Format(TextBox5.Text, "###,###,###") end sub

我尽量避免没有一堆文本框子。示例

Private Sub TextBox1_Change() TextBox1.Text = Format(TextBox1.Text, "###,###,###") End Sub

然后是另一个

Private Sub TextBox2_Change() TextBox2.Text = Format(TextBox2.Text, "###,###,###") End Sub

和另一个

Private Sub TextBox15_Change() TextBox15.Text = Format(TextBox15.Text, "###,###,###") End Sub

希望它有意义。谢谢

我试过了:

Private Sub TextBox1_Change() TextBox1 = Format(TextBox1.Value, "###,###,###") TextBox2 = Format(TextBox1.Value, "###,###,###") TextBox5 = Format(TextBox1.Value, "###,###,###") TextBox10 = Format(TextBox1.Value, "###,###,###") End Sub

但它不起作用。

我还尝试创建一个通用的子名称并放入格式代码,但也没有成功。

如果有人能帮我,我将不胜感激。

谢谢。

如果这是Access,则需要设置文本框的.Format属性。您只是想将文本框对象设置为一种格式。对象的格式不能相等。试试这样的东西:TextBoxt.Format("###,###,###")

如果这是不同的Office应用程序VBA,则您将希望格式化文本,并将文本框的.Text属性设置为新格式化的文本,如下所示:TextBox1.Text = Format(TextBox1.Text, "###,###,###")


关于我在下面的评论。为什么不直接使用工作簿打开事件?

Private Sub Workbook_Open()
    TextBox1 = Format(TextBox1.Value, "###,###,###")
    TextBox2 = Format(TextBox1.Value, "###,###,###")
    TextBox5 = Format(TextBox1.Value, "###,###,###")
    TextBox10 = Format(TextBox1.Value, "###,###,###")
End Sub

最新更新