Solved

Where should I format my Excel text box controls in my VBA code ?

Posted on 2013-10-26
4
1,609 Views
Last Modified: 2013-10-26
Hi All,

I've created an Excel 2010 User form and I've been trying to format the VBA text box controls as currency fields :

      Me.Controls("teUnitPrice" & i).Value = Format(Me.Controls("teUnitPrice" & i).Value, "#,##0.00")

But if I enter £300, for example, it doesn't autofill the pence zeroes as I would expect.

Is there a quicker or a better means of applying currency formats to control fields than formatting them individually ? Can they be formatted in Properties ?

Thanks
Toco
0
Comment
Question by:Tocogroup
  • 2
  • 2
4 Comments
 
LVL 14

Expert Comment

by:Faustulus
ID: 39602755
Hi Toco,
No, you can't set a number format for a TextBox because, well, it is a Text box. But I am surprised that your code doesn't work. Try this variation:-
Me.Controls("teUnitPrice" & i).Text = Format(Me.Controls("teUnitPrice" & i).Value, "#,##0.00")
0
 

Author Comment

by:Tocogroup
ID: 39602774
Hi Faustalus,

Yes that worked, although it may have been because I only formatted the field in my Initialize procedure, and not in the procedure which populated the control from the array. I was hoping I could format the control just once at startup and it would retain it for other procedures.

I guess there isn't such a thing as a Number box is there ? There should be as it would be useful.

How do I combine these two lines ?

      Me.Controls("teNet" & i) = SalesItemsArray(x, 5)
      Me.Controls("teNet" & i).Value = Format(Me.Controls("teNet" & i).Value, "#,##0.00")

Thanks
Toco
0
 
LVL 14

Accepted Solution

by:
Faustulus earned 500 total points
ID: 39602787
Yeah, you got the analysis down correctly. You have to do every item. But once you have it done you tend not to miss the "NumberBox" so much any more, :lol

The concatenation of the two numbers manipulations should be,
Me.Controls("teNet" & i).Value = Format(SalesItemsArray(x, 5), "#,##0.00")
0
 

Author Closing Comment

by:Tocogroup
ID: 39602796
That's great. I can use that structure all the time now. Previously I've always done it separately.

Thanks
Toco
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question