Trying to make a UserForm calculator - decimal issues

TessieBear99

New Member
Joined
Aug 26, 2018
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I've made a small basic calculator simply to subtract the GST component from a GST-inclusive number (for those who aren't Australian, GST is Goods & Services Tax which is a 10% tax on certain items sold, so I have the Y and I'm trying to find the X in "X+10%=Y").

I have the calculation which is the GST incl. number divided by 11 and multiplied by 10 (e.g. 100/11 = 9.09 * 10 = 90.91) and I think this is working because it gives me a partially accurate result but the decimals aren't working, at least I think this is my issue. I will put 100 in the entry box and click = and it will spit out 90.00 (which we know is wrong, but close).

I've formatted the textboxes so when they're updated they become "#,##0.00".

Object names are...
UserForm name: frmGSTCalc
First text box name: txtEntry
Second text box name: txtCalc
" = " button name: cmdCalc

Here's my code:
VBA Code:
Private Sub cmdCalc_Click()

Dim total As Long
Dim GST As Long

GST = Val(txtEntry.Value) / 11
total = GST * 10
txtCalc = total

    
End Sub

Private Sub txtCalc_Change()

    Me.txtCalc = Format(Me.txtCalc, "#,##0.00")

End Sub

Private Sub txtEntry_AfterUpdate()

    Me.txtEntry = Format(Me.txtEntry, "#,##0.00")

End Sub

Here's what it looks like:
Capture.PNG


Any help would be greatly appreciated!

Thanks :)
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
GST is declared as Long, which is an integer type, so you are losing any decimal portion when you do the division by 11. Declare it as Currency to get four digits of precision after the decimal. Declare it as Double to get maximum precision (but I don't think you need that since you should probably also be rounding to nearest cent; I don't know how the government rounds on GST).
 
Upvote 0
Solution
GST is declared as Long, which is an integer type, so you are losing any decimal portion when you do the division by 11. Declare it as Currency to get four digits of precision after the decimal. Declare it as Double to get maximum precision (but I don't think you need that since you should probably also be rounding to nearest cent; I don't know how the government rounds on GST).
You're a genius! Thank you! I knew it was going to be something like that but couldn't figure it out. Thanks so much :giggle:
 
Upvote 0

Forum statistics

Threads
1,215,052
Messages
6,122,878
Members
449,097
Latest member
dbomb1414

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