Solved

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

Posted on 2013-10-26
4
1,669 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

623 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