用户表单文本框中的格式编号 [英] Format number in userform textbox

查看:62
本文介绍了用户表单文本框中的格式编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用户窗体中有一个texbox,用户可以在其中输入数字.这些数字将被转移到Excel电子表格中进行计算.

I have a texbox in a userform in which the user will input numbers. Those numbers will be tranfered to an Excel spreadsheet for calculation.

我希望当用户在文本框中输入数字时,它以特定格式显示为数字.

I want that when the user inputs numbers in the Textbox it displays as a number in a specific format.

例如:2000000,我希望它在文本框中为2,000,000.00.

For example: 2000000, I want it as 2,000,000.00 in the Textbox.

我尝试过:

Public Sub UserForm_Initialize()    
    TextBox1 = Format(TextBox1, "$#,##0.00")
End Sub

但是用户窗体没有改变.

But the Userform doesn't change.

如果我尝试以下操作

Private Sub TextBox1_Change()
    TextBox1 = Format(TextBox1, "$#,##0.00")
End Sub

用户窗体可以充分显示该数字,但随后使用文本格式(而不是数字)更新Excel电子表格,并且计算不会运行.

The userform shows the number adequately, but then updates the Excel spreadsheet with a text format instead of number and the calculation doesn't run.

推荐答案

这是一种解决方法,请使用第二个子例程以字符串格式获取值.

Here is a workaround, use the second subroutine to get the value in a string format.

Private Sub TextBox1_Change()
    TextBox1 = Format(TextBox1, "$#,##0.00")
End Sub

然后转换分配该值的单元格的格式. Range("A1").Value = CDbl(Range("A1").Value)

Then convert the format of the cell where this value was assigned. Range("A1").Value = CDbl(Range("A1").Value)

这篇关于用户表单文本框中的格式编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆