Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Simple addition and multiplication of textbox values

0

I have these codes to add and multiply textboxes but I'm not coming up with the correct amount in the total textbox.  It ends up being more than what it should so how do I write this to get the correct amount? I have searched and can't find nothing to fit for what I'm looking for. In the attached file on the userform "frmGunParts" that calculates the cost of parts.

Private Sub txtMTLcst_Change()
    
    If txtMTLcst.Value = "" Then txtMTLcst.Value = 0
    If txtLBRcst.Value = "" Then txtLBRcst.Value = 0
    If txtQTY.Value = "" Then txtQTY.Value = 0
    txtTOTALcst.Value = (Val(txtMTLcst.Value) + Val(txtLBRcst.Value)) * Val(txtQTY.Value)

    
    sh.Cells(iRow + 1, 6) = txtMTLcst.Value
    txtMTLcst = Format(txtMTLcst.Value, "$###,##")
End Sub
Private Sub txtLBRcst_Change()

    If txtMTLcst.Value = "" Then txtMTLcst.Value = 0
    If txtLBRcst.Value = "" Then txtLBRcst.Value = 0
    If txtQTY.Value = "" Then txtQTY.Value = 0
    txtTOTALcst.Value = (Val(txtMTLcst.Value) + Val(txtLBRcst.Value)) * Val(txtQTY.Value)


    sh.Cells(iRow + 1, 9) = txtLBRcst.Value
End Sub
Answer
Discuss

Answers

0
Selected Answer

Hi again Divinedar.

The multiplications work correctly if you convert your text box values to the Double data type first (or to Decimal using CDec). In the code below, the VBA function CDbl() does that (and also I did that for other procedures behind that form). 

Private Sub txtMTLcst_Change()

    If txtMTLcst.Value = "" Then txtMTLcst.Value = 0
    If txtLBRcst.Value = "" Then txtLBRcst.Value = 0
    If txtQTY.Value = "" Then txtQTY.Value = 0
    txtTOTALcst.Value = (CDbl(txtMTLcst.Value) + CDbl(txtLBRcst.Value)) * CDbl(txtQTY.Value)


    sh.Cells(iRow + 1, 6) = txtMTLcst.Value
    'txtMTLcst = Format(txtMTLcst.Value, "$###,##")
End Sub
Private Sub txtLBRhrs_Change()

    If txtLBRhrs.Value = "" Then txtLBRhrs.Value = 0
    If txtPerHRCost.Value = "" Then txtPerHRCost.Value = 0
    txtLBRcst.Value = (CDbl(txtLBRhrs.Value) * CDbl(txtPerHRCost.Value))

    sh.Cells(iRow + 1, 7) = txtLBRhrs.Value

End Sub

Note that I commented out the line formatting Material Cost to "$###,##" (which would require extra code to detect and remove the $ from the string before multiplications were done) and instead changed your form labels to read MTL Cost($) etc.

Also, I added these (repeated) bold bits so the total cost is recalculated if you later change the quantity:

Private Sub txtQTY_Change()
    sh.Cells(iRow + 1, 5) = txtQTY.Value
    If txtMTLcst.Value = "" Then txtMTLcst.Value = 0
    If txtLBRcst.Value = "" Then txtLBRcst.Value = 0
    txtTOTALcst.Value = (CDbl(txtMTLcst.Value) + CDbl(txtLBRcst.Value)) * CDbl(txtQTY.Value)
End Sub

Minor thing- I corrected the heading of column F of the Customer_Order_History worksheet  to read QUANTITY.

I guess in time your labour rate might be a constant and that material costs / labour hours will be stored in you PT- worksheets (and gathered/ presented in the form) but I leave that to you.

Hope this fixes things for you.

Discuss

Discussion

So totally cool.  Thank you. Still got more to go but I'm learning when writing this from scratch.
Divinedar (rep: 20) Mar 13, '23 at 4:46 pm
Glad that worked for you, Divinedar. Thanks for selecting my Answer. 
John_Ru (rep: 6142) Mar 13, '23 at 5:39 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login