Share via

I cannot get VBA in Excel to make the Userform Calculator i created to perform operations on numbers with decimals right

Joshua Mcdowall 20 Reputation points
2026-05-10T08:01:25.4+00:00

Private Sub cmbdecimal_Click()

If InStr(txtDisplay.Text, ".") = 0 Then

txtDisplay.Text = txtDisplay.Text + "."

End If

End Sub

^This is the code i used on the command button for "."

I can type a decimal, but the answer the calculator produces doesn't have any decimal point/places.

Help me please, I'm on my knees.

Developer technologies | Visual Basic for Applications
0 comments No comments

Answer accepted by question author

  1. Surya Amrutha Vaishnavi Lanka (INFOSYS LIMITED) 2,275 Reputation points Microsoft External Staff
    2026-05-11T12:16:54.7866667+00:00

    Thanks for reaching out!

    The problem is not with your decimal button code.

    The issue happens because the calculator is

    • Treating the input values as text instead of numbers, or
    • Converting them to an integer data type, which removes the decimal portion.

    Below are some steps which are helpful to solve the issue

    • Make sure all variables used in calculations are defined as Double, not Integer.
    • Before calculation, explicitly convert textbox input to numbers using CDbl().
    • Perform calculation using Double type
    • Ensure display formatting, for output to show decimals

    Please let us know if you require any further assistance, we’re happy to help. If you found this information useful, kindly mark this as "Accept Answer". So that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    Was this answer helpful?

    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Q&A Assist
    2026-05-10T08:01:37.97+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    The decimal is being added correctly to the text in txtDisplay, but the calculation logic in the rest of the code is almost certainly converting the text to a numeric type that does not preserve decimals (for example, Integer, Long, or using integer division), or formatting the result with zero decimal places.

    To make the calculator work with decimals, ensure all of the following in the calculation code (not just the decimal button):

    1. Use a data type that supports decimals
      • Declare variables that hold numbers as Double (or as Variant and assign decimal values) instead of Integer or Long.
      • Example:
             Dim firstNumber As Double
             Dim secondNumber As Double
             Dim result As Double
        
    2. Use normal division, not integer division
      • Use / for division, not \.
      • \ performs integer division and discards the decimal part.
      • Example:
             result = firstNumber / secondNumber   ' keeps decimals
             ' avoid: result = firstNumber \ secondNumber
        
    3. Convert the textbox text to a decimal-capable type
      • When reading from txtDisplay.Text, convert to Double (or CDec into a Variant with Decimal subtype):
             firstNumber = CDbl(txtDisplay.Text)
             ' or
             firstNumber = CDec(txtDisplay.Text)
        
    4. Do not format the result to zero decimal places
      • If using Format or FormatNumber, make sure the format allows decimals, for example:
             txtDisplay.Text = Format(result, "0.############")
             ' or simply
             txtDisplay.Text = CStr(result)
        
    5. Ensure Excel’s decimal separator matches the input
      • If the system uses a comma as decimal separator but the calculator uses a dot (.), either:
        • Set Excel to use . as decimal separator, or
        • Replace . with the system separator before conversion.
      • In VBA, the decimal separator used by Excel can be controlled via Application.DecimalSeparator and Application.UseSystemSeparators:
             Application.DecimalSeparator = "."
             Application.UseSystemSeparators = False
        
        This forces Excel to treat . as the decimal separator.

    The cmbdecimal_Click code itself is fine for inserting a single decimal point. The missing decimals in the result come from how the numbers are stored, converted, divided, or formatted elsewhere in the calculator code.


    References:

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.