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

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
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
 
Upvote 0

Forum statistics

Threads
1,214,657
Messages
6,120,773
Members
448,991
Latest member
Hanakoro

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