Equation Miscaculation

hmk

Active Member
Joined
Jun 8, 2004
Messages
423
Hi there
When I put this formating to the value of TextBoxs,

Code:
Gtot.Value = Format(Gtot.Value, "#,##0.000")
tot.Value = Format(tot.Value, "#,##0.000")
bal.Value = Format(bal.Value, "#,##0.000")
However, when i cancel the formating then the equation gives correct result but with no comma nor period.....which are realy needed

Then the result of this line is always 1 or not correct
Code:
bal.Value = val(Gtot.Value) - val(tot.Value)

How can get the correct result out of equation ?

The whole code:

Code:
Private Sub cmdAdd_Click()

MyCalc
tot.Value = ""

End Sub

Private Sub bal_Change()
bal.Value = Format(bal.Value, "#,##0")
End Sub

Private Sub tot_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'tot.Value = Format(tot.Value, "#,##0.000")
    With Me.ActiveControl
        If Not IsNumeric(.Value) And .Value <> vbNullString Then
            MsgBox "Sorry, only numbers allowed"
            .Value = vbNullString
        ElseIf Left(.Value, 1) = "-" And IsNumeric(Left(.Value, 2)) Then
            Exit Sub
        End If
    End With
End Sub

Private Sub Gtot_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'Gtot.Value = Format(Gtot.Value, "#,##0.000")
    With Me.ActiveControl
        If Not IsNumeric(.Value) And .Value <> vbNullString Then
            MsgBox "Sorry, only numbers allowed"
            .Value = vbNullString
        ElseIf Left(.Value, 1) = "-" And IsNumeric(Left(.Value, 2)) Then
            Exit Sub
        End If
    End With
End Sub

Sub MyCalc()
If tot.Value = "" Then Exit Sub

If tot.Value <> 0 Then
bal.Value = val(Gtot.Value) - val(tot.Value)
    Exit Sub
End If

End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

BrianB

Well-known Member
Joined
Feb 17, 2003
Messages
8,127
This seems to work correctly :-
Code:
Private Sub CommandButton1_Click()
    gtot.Value = Format(gtot.Value, "#,##0.000")
    tot.Value = Format(tot.Value, "#,##0.000")
    '---------------------------------------------
    bal.Value = CDbl(gtot.Value) - CDbl(tot.Value)
    bal.Value = Format(bal.Value, "#,##0.000")
End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,499
Messages
5,832,068
Members
430,109
Latest member
tinezi

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
Top