PDA

View Full Version : How to Format a TextBox bound to a ControlSource?



kdberg
01-08-2009, 10:26 PM
I have a UserForm in Excel 2007 with a textbox that is bound to a worksheet cell. The worksheet cell is formatted to hold a number and to display it as fixed point with one decimal point precison. As an example it might contain the value 1, and will display it as 1.0 as it should.

The problem I have is that I can't get the textbox on the form to display it as fixed decimal with one decimal point precision. If the worksheet cell is an integer value (such as 1.0) the textbox will display 1, not 1.0.

If the worksheet cell value is not an integer, say 2.1, then the text box will also display 2.1. So the problem is only with integer values. Is there a way to force the textbox to format the way that I want? The valid range of values is 0 to 999.9 .

georgiboy
01-09-2009, 02:35 AM
You will need to use format like this...

TextBox1.Value = Format(Range("A1").Value, "0.0")

The red is just an example range.

Hope this helps.

Bob Phillips
01-09-2009, 03:39 AM
I would remove the data binding myself, but if you keep it you need to set



TextBox1.Value = Format(TextBox1.Value, "0.0")


but you would need to work out where in the code that you insert that formatting, when is it appropriate.

kdberg
01-09-2009, 09:09 AM
Thanks for the responses.

Could you please explain your preference for not using the data binding feature of sourcecontrol and writing all the equivalent code yourself? Did you mean in just this particular case or in general?

Bob Phillips
01-09-2009, 09:27 AM
I mean in general. I find binding to a worksheet to be a pain, so I always load listboxes, textboxes, etc., myself in code.