Calculating with textboxes....

M

Mark Rosenkrantz

Dear all;

I have a UserForm with a few textboxes.
The first textbox : tbAmount holds an invoice amount.
The second textbox: tbVAT should display the amount of VAT ( 19 %
of the amount )

The formula I use is: .tbVAT.Value = .tbAmount.Value * 0.19

Looks fine to me.
But, the stupid box don't disply the right amount.
I live in Europe and the Regional settings are a dot ( . ) for the thousands
seporator and a comma ( , ) for the decimal seporator.

When I enter 2250,00 ( or 2250 ) in .tbAmount, then textbox .tbVat displays
:
427.5 which is not what I want as I hope to see :
427,50

When I bring these values over to the worksheet it displays :
? 2250,00 which is OK, and
? 4275,00 which is not OK, as I want to see:
? 427,50

Apparantly there is some error in the way the textboxes work with thousands.
What is wrong with me ???? ( assuming I am not perfect )
I can't see what I did wrong.

Mark.
 
S

Steve Garman

Mark said:
Dear all;

I have a UserForm with a few textboxes.
The first textbox : tbAmount holds an invoice amount.
The second textbox: tbVAT should display the amount of VAT ( 19 %
of the amount )

The formula I use is: .tbVAT.Value = .tbAmount.Value * 0.19
Try:

..tbVAT.Value = FormatNumber(.tbAmount.Value * 0.19,2)
 
H

Harald Staff

Hi Mark

Don't trust a textbox's value property. Convert its text to number before calculation:

Sub test()
MsgBox CDbl("1.234,56")
End Sub

Dim R As Double
R = CDbl(.tbAmount.Text) * 0.19
..tbVat.Text = Format(R, "#,###.##")

Note that Format and VBA calculations use the american logic , for thousands and . for
decimals. You'll get used to it after a while.

-
HTH. Best wishes Harald
Followup to newsgroup only please.
 
M

Mark Rosenkrantz

Thanks Harald and Steve.



Harald Staff said:
Hi Mark

Don't trust a textbox's value property. Convert its text to number before calculation:

Sub test()
MsgBox CDbl("1.234,56")
End Sub

Dim R As Double
R = CDbl(.tbAmount.Text) * 0.19
.tbVat.Text = Format(R, "#,###.##")

Note that Format and VBA calculations use the american logic , for thousands and . for
decimals. You'll get used to it after a while.

-
HTH. Best wishes Harald
Followup to newsgroup only please.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top