2

I am calculating a bunch of values like density and volume. I show them on a form and when the user clicks "Update", I save that number to a worksheet.

There are a lot of numbers, so I want to neatly display the rounded version without losing the better precision number.

Like

Sub SetDensity()

  dim rDensity as double

  rDensity = 0.78022134

  me.txtDensity = Format(rDensity,"0.00")  ' user sees 0.78

End Sub

When data is finally written to worksheet, I want to be able to store the full precision like:

Sub UpdateWS()    
  Sheets(1).Range("A:A") = me.txtDensity   ' put value 0.78022134 on worksheet
End Sub

Does the textbox have an alternate field where I can store the full number without displaying it, or do I need a hidden textbox for every real number that contains the full value?

Thanks

4

1 回答 1

1

您可以使用TextBox'Tag属性来存储值。这是一个简单的例子:

Private Sub UserForm_Activate()
Me.TextBox1.Tag = 2.38232
Me.TextBox1.Value = Format(Me.TextBox1.Tag, "0.00")
Me.TextBox2.Tag = 4.99999
Me.TextBox2.Value = Format(Me.TextBox2.Tag, "0.00")
End Sub

Private Sub CommandButton1_Click()
MsgBox Me.TextBox1.Tag * Me.TextBox2.Tag
End Sub

在这种情况下,您理想地做的是创建一个用户表单的实例并将值作为 传递给它Properties,然后当用户单击“确定”时,您将计算出的值作为另一个返回Form Property。这是我网站上的一篇文章,还有一篇来自Daily Dose of Excel 的文章,可以为您提供总体思路。您可以使用这些概念并进行修改以使用文本框。

于 2013-08-12T22:52:33.323 回答