ActiveX Input Format as currency with 2dp

JonReyno

Board Regular
Joined
Jun 8, 2009
Messages
130
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have been trawling the web and Mr Excel for an answer, but everything I have tried doesn't work. I am creating a userform that users need to be able to add in monetary values. Below is how I have the textbox set up at the moment, but it's not doing what I want:

VBA Code:
Private Sub TextBox18_Change()
    TextBox18.Value = Format(TextBox18.Value, "###,##")
End Sub

If I wanted to enter the value 1,234.56 into the cell I can't. It will give me 1,234 but doesn't all the .56. So I have tried different variations on it, commonly "###,##0.00" but that keep giving me 1.00 as a result.

Does anyone have a stroke of genius that will help me out? I don't mind if the filed has £ at the front or not. Currently I have that outside the box, but it can be part of the solution if needs be.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try putting it in the exit event rather than a change event
VBA Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
 
Upvote 0
Try putting it in the exit event rather than a change event
VBA Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

That's worked for the 2dp, but I seem to have lost the auto "," in the thousands which I'd like to maintain as it's clearer to read for values like 100000. I thought the problem was with my format not the sub itself. I didn't think to alter that!
 
Upvote 0
This works for me
VBA Code:
Private Sub TextBox18_Exit(ByVal Cancel As MSForms.ReturnBoolean)
   Me.TextBox18.Value = Format(Me.TextBox18.Value, "#,##0.00")
End Sub
 
Upvote 0
This works for me
VBA Code:
Private Sub TextBox18_Exit(ByVal Cancel As MSForms.ReturnBoolean)
   Me.TextBox18.Value = Format(Me.TextBox18.Value, "#,##0.00")
End Sub
That's very strange, it still doesn't put the comma back in. I have copied your script and pasted it in, so not sure why it wont format it in the same way.
 
Upvote 0
Have you removed the change event code?
 
Upvote 0
Have you removed the change event code?
Yeah, I literally copy and pasted what you have there. Does it need to go into the worksheet code (which is where I have it) or in a workbook Module? I also have a few textboxes which are all separate Subs. Would that cause a problem?
 
Upvote 0
It needs to go in the UserForm module
 
Upvote 0
Ah, I don't know I have called what I am doing the right thing! All I have done is entered a load of ActiveX textboxes into the worksheet in a form like fashion. It's not based on the UserForm that's under VBAProject. Does that make a difference?
 
Upvote 0
Yes that does make a difference, as ActiveX textboxes do not have an Exit event.
I don't use controls on sheets, so not sure how to do it I'm afraid.
 
Upvote 0

Forum statistics

Threads
1,215,695
Messages
6,126,261
Members
449,307
Latest member
Andile

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top