Solved

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

Posted on 2013-10-26
4
1,582 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now