won't let me format with textbox

rlink_23

Board Regular
Joined
Oct 30, 2015
Messages
149
On this userform I have been working on, basically I have TextBox1 + TextBox2 = Subtotal, Then subtotal + TextBox3 = Grand Total

Which the code looks like this

Code:
Private Sub TextBoxSum()    Dim Total As Double
    Total = 0
    If Len(TextBox1.Value) > 0 Then Total = Total + Val(TextBox1Value)
    If Len(TextBox2.Value) > 0 Then Total = Total + Val(TextBox2.Value)
    
    
    Subtotal.Value = Total
    
    
End Sub

Then I have
Code:
Private Function NumericOnly(ByVal KeyAscii As MSForms.ReturnInteger) As MSForms.ReturnInteger   Dim Key As MSForms.ReturnInteger
   Select Case KeyAscii
        Case 46, 48 To 57
               Set Key = KeyAscii
        Case Else
             KeyAscii = 0
                Set Key = KeyAscii
        End Select
        Set NumericOnly = Key
End Function

Then each corresponding TextBox_ keypress is this

Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)KeyAscii = NumericOnly(KeyAscii)
End Sub

Then lastly i have the change event for eatch textbox as
Code:
Private Sub TextBox1_Change()TextBoxSum
End Sub

This seems like a lot of typing for such a small task but All I am trying to do, and I cannot figure it out for the lkife of me is I want to format TextBox1 like this
textBox1.Value = Format(TextBox1.Value, "$#,##0.00")

Which is fine it formats to that but it quits summing the textboxes once added. I want it to format AND sum the texboxes but I am failing at this simple task and thought I would ask for help beofre I rip my hair out lol... Any thoughts would be greatly appreciated :) Thank you so kindly
 
This is what I have in the the TextBox1 and TextBox2 Change event

Code:
Private Sub TextBox1_Change()
TextBox1.Value = Format(TextBox1.Value, "$#,##0.00")
TextBoxSum


End Sub


And I have tried both before and after the TextBoxSum
 
Last edited:
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
This is what I have in the the TextBox1 and TextBox2 Change event

Code:
Private Sub TextBox1_Change()
TextBox1.Value = Format(TextBox1.Value, "$#,##0.00")
TextBoxSum


End Sub


And I have tried both before and after the TextBoxSum


Try replacing those events with AfterUpdate event

Code:
Private Sub TextBox1_AfterUpdate()
    TextBox1.Value = Format(TextBox1.Value, "$#,##0.00")
    TextBoxSum
End Sub


Private Sub TextBox2_AfterUpdate()
    TextBox2.Value = Format(TextBox2.Value, "$#,##0.00")
    TextBoxSum
End Sub

Dave
 
Upvote 0
With this it doesn't even change the formatting at any point in textbox1 and textbox2...

the event applies calc & formatting after you exit the control.

If you want textboxes to update as you enter data then try this

Code:
Private Sub TextBox1_Change()
    TextBoxSum
End Sub


Private Sub TextBox2_Change()
    TextBoxSum
End Sub


Private Sub TextBox1_AfterUpdate()
    TextBox1.Value = Format(TextBox1.Value, "$#,##0.00")
End Sub


Private Sub TextBox2_AfterUpdate()
    TextBox2.Value = Format(TextBox2.Value, "$#,##0.00")
End Sub


this should sum the textboxes and apply formatting after you exit the control.

Dave
 
Upvote 0
It took me a couple days to get back to this part of the project but this is not working still... I am losing my mind.... lol
 
Upvote 0
how I made it work if y'all care!! lol I am stoked


Code:
Private Sub TextBoxSum()

    Dim Total As Double


    Total = 0


    If Len(TextBox1.Value) > 0 Then Total = Total + (Me.TextBox1.Text)


    If Len(TextBox2.Value) > 0 Then Total = Total + (Me.TextBox2.Text)


    


    Subtotal.Value = Total


   engineprice.Value = Format(TextBox1.Value, "$#,###.00")
   Core.Value = Format(TexctBox2.Value, "$#,###.00")


End Sub

Seems the Val function was the culprit, Which is crazy that it was something sooooooo simple!!! Thank you for the help guys!!! As always much appreciation!!!!!
I have another tread posted today if you have any ideas on that one too lolol

[SOLVED]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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