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

Userform textboxes to calculate and re-calculate

0

I manage to get my textboxes to calculate but I am unable to get the correct results. I need help with getting the correct results.  The code below is that when I put a value in the textbox "txtPerHRCost" it does the calculation correctly for "txtLBRcst.Value" but it also adds the value put in the textbox "txtPerHRCost" to the textbox "txtTOTALcst.Value" TOTAL COST.

'Get Labor Total
Private Sub txtLBRhrs_Change()
    'Set textbox for calculation and will re-calculate if changed
        If txtLBRhrs.Value = "" Then txtLBRhrs.Value = 0
        If txtPerHRCost.Value = "" Then txtPerHRCost.Value = 0
        txtLBRcst.Value = (CDbl(txtLBRhrs.Value) * CDbl(txtPerHRCost.Value))
        txtLBRcst.Value = Format(WorksheetFunction.RoundUp(txtLBRcst.Value, 2), "###.00")
End Sub
Private Sub txtPerHRCost_Change()
    'Set textbox for calculation and will re-calculate if changed
        If txtLBRhrs.Value = "" Then txtLBRhrs.Value = 0
        If txtPerHRCost.Value = "" Then txtPerHRCost.Value = 0
        txtLBRcst.Value = (CDbl(txtLBRhrs.Value) * CDbl(txtPerHRCost.Value))
        txtLBRcst.Value = Format(WorksheetFunction.RoundUp(txtLBRcst.Value, 2), "###.00")
End Sub

Looks like my problem isn't in the codes above but could be wherever txtTOTALcst.Value is recalculated.

I also need help on making sure the calculations in the  other fields are calculating correctly.

The file is attached.

Answer
Discuss

Discussion

Hi again Divinedar. Hope Willie or someone can help- I can't look at this before Friday.   Looks like your problem isn't in these codes but wherever txtTOTALcst.Value is recalculated. I can't read that macro (on my mobile phone) but it would help others if you add that to your question. 
John_Ru (rep: 6142) Apr 12, '23 at 3:28 pm
Thank you I edited my question and will review it in my file.
Divinedar (rep: 20) Apr 12, '23 at 7:07 pm
@John
Hi John, I know you are already familiar with this file of Divinedar's so I will leave it to you - I don't have the time to acquaint myself with everything that is going on.
WillieD24 (rep: 557) Apr 12, '23 at 9:34 pm
Thank you. I can wait on John.
Divinedar (rep: 20) Apr 13, '23 at 10:28 am
Add to Discussion

Answers

0
Selected Answer

Divinedar

Quick answer (without time to test)- please check these subs- the seem to make material totals ito INCLUDE the labour cost (see bits in bold):

'====================================CALCULATION OF TEXTBOXES ON USERFORM============================================
'Get Material Cost
Private Sub txtQTY_Change()
    'Set textbox for calculation and will re-calculate if changed
        If txtMTLcst.Value = "" Then txtMTLcst.Value = 0
        If txtLBRcst.Value = "" Then txtLBRcst.Value = 0
        If txtQTY.Value = "" Then txtQTY.Value = 0
        txtMTLtlt.Value = ((CDbl(txtMTLcst.Value) * CDbl(txtQTY.Value)) + CDbl(txtLBRcst.Value))
        txtMTLtlt.Value = Format(WorksheetFunction.RoundUp(txtMTLtlt.Value, 2), "###.00")
End Sub

Private Sub txtMTLcst_Change()
    'Set textbox for calculation and will re-calculate if changed
        If txtMTLcst.Value = "" Then txtMTLcst.Value = 0
        If txtLBRcst.Value = "" Then txtLBRcst.Value = 0
        If txtQTY.Value = "" Then txtQTY.Value = 0
        txtMTLtlt.Value = ((CDbl(txtMTLcst.Value) * CDbl(txtQTY.Value)) + CDbl(txtLBRcst.Value))
        txtMTLtlt.Value = Format(WorksheetFunction.RoundUp(txtMTLtlt.Value, 2), "###.00")
End Sub
Private Sub txtLBRcst_Change()
    'Set textbox for calculation and will re-calculate if changed
        If txtMTLcst.Value = "" Then txtMTLcst.Value = 0
        If txtLBRcst.Value = "" Then txtLBRcst.Value = 0
        If txtQTY.Value = "" Then txtQTY.Value = 0
        txtMTLtlt.Value = ((CDbl(txtMTLcst.Value) * CDbl(txtQTY.Value)) + CDbl(txtLBRcst.Value))
        txtMTLtlt.Value = Format(WorksheetFunction.RoundUp(txtMTLtlt.Value, 2), "###.00")
End Sub

Can that be right?

REVISION 17 March 2023 (In response to user's revised file and discussion point below):

You said 'This is where I get the error

        txtDiscAmt.Value = (CDbl(txtTOTALcst.Value) * CDbl(txtDiscPer.Value.Value)) / 100

and it arises from the .Value.Value bit. However the calculation is wrong- the discount should be calculated as a prescentage of your cost PLUS your negotiaton markup values. I've corrected that in the attached revised file as follows (first bold line):

'Get Discount Amount Total
Private Sub txtDiscPer_Change()
    'Set textbox for calculation and will re-calculate if changed
        If txtTOTALcst.Value = "" Then txtTOTALcst.Value = 0
    ' make this a default value
        If txtDiscPer.Value = "" Then txtDiscPer.Value = 5
        ' ### error line with wrong calculation
        'txtDiscAmt.Value = (CDbl(txtTOTALcst.Value) * CDbl(txtDiscPer.Value.Value)) / 100
        txtDiscAmt.Value = (CDbl(txtTOTALcst.Value) + CDbl(txtNegAmt.Value)) * CDbl(txtDiscPer.Value) / 100
    'format that to 2 decimal places (change decimal marker if needed)
        txtDiscAmt.Value = Format(WorksheetFunction.RoundUp(txtDiscAmt.Value, 2), "#,##0.00")
End Sub

and in the second line I've corrected the formatting so it rounds up first then forces two decimal places (so you see 0.00 and 5.40 rather than .00 and 5.4 for example). I've done that in other procedures too.

That macro causes the Discount amount to change and I've changed that sub as follows:

Private Sub txtDiscAmt_Change()
    'Set textbox for calculation and will re-calculate if changed
        If txtTOTALcst.Value = "" Then txtTOTALcst.Value = 0
    ' make this a default value
        If txtNegAmt.Value = "" Then txtNegAmt.Value = 0
        If txtDiscAmt.Value = "" Then txtDiscAmt.Value = 0
        txtSellAmt.Value = (CDbl(txtTOTALcst.Value) + CDbl(txtNegAmt.Value)) - CDbl(txtDiscAmt.Value)
    ' format that to 2 decimal places (change decimal marker if needed)
        '### CORRECTED
        txtSellAmt.Value = Format(WorksheetFunction.RoundUp(txtSellAmt.Value, 2), "#,##0.00")

    'check price is good
    Call SellCheck
End Sub

where the lines in bold call a new sub:

Private Sub SellCheck()

    ' check if a loss is made (make background red)
    If CDbl(txtSellAmt.Value) < CDbl(txtTOTALcst.Value) Then
        txtSellAmt.BackColor = 8421631
        Else
        txtSellAmt.BackColor = -2147483643
    End If

End Sub

This causes the selling price textbox to turn red if you give too much discount. E.g. if your total cost is ASD 100.00 say, a 10% markup would give a gross price of ASD 110 but a 10% discount would knock off ASD 11 giving a nett price of ASD 99 (so less than cost).I've done similar for the markup code too.

Note that I've commented out your code for 

'Private Sub txtSellAmt_Change()

    ' ### Commented out this code for the result value (which otherwise changes and retriggers itself)

for the reason above. In fact, a single change in value on your userform already causes multiple macros to be triggered (but it's too fast for you to notice, you just see live updates).

Hope this fixes things for you.

Discuss

Discussion

I figured it out trying to figure the discount amount formula 
Divinedar (rep: 20) Apr 13, '23 at 1:19 pm
I'll post my corrections 
Divinedar (rep: 20) Apr 13, '23 at 1:20 pm
File Name: Calculate textboxes and re-calculate v02 I got the before mentioned code above to work and you were right had to take out the labor code. I can't get the following to calculate. I need to get the correct response from the following codes: 'Get Sell Amount Total
Private Sub txtNegAmt_Change() Private Sub txtDiscAmt_Change() Private Sub txtSellAmt_Change()

I get an error in this code when I try to get the default value of the “txtDiscPer.Value” to fill in once you start filling in the textboxes.
'Get Discount Amount Total
Private Sub txtDiscPer_Change()
    'Set textbox for calculation and will re-calculate if changed
        If txtTOTALcst.Value = "" Then txtTOTALcst.Value = 0
    ' make this a default value
        If txtDiscPer.Value = "" Then txtDiscPer.Value = 5
‘This is where I get the error
        txtDiscAmt.Value = (CDbl(txtTOTALcst.Value) * CDbl(txtDiscPer.Value.Value)) / 100
    'format that to 2 decimal places (change decimal marker if needed)
        txtDiscAmt.Value = Format(WorksheetFunction.RoundUp(txtNegAmt.Value, 2)) ', "###.00")
 End Sub
I can’t figure out where I’m going wrong. I need to calculate the following textboxes: txtDiscPer
txtDiscAmt
txtSellAmt
I attached a version 02 file
 
Divinedar (rep: 20) Apr 16, '23 at 4:35 pm
Please see my answer with REVISION 17 March 2023 (and revised file)
John_Ru (rep: 6142) Apr 17, '23 at 8:07 am
Thank you awesome. IT WORKS. Thanks for the "SellCheck" code it wll come in handy.
Divinedar (rep: 20) Apr 17, '23 at 5:06 pm
Glad that worked for you. I made SellCheck since you're using markup (rather than margin) so discounts can be confusing. Thanks for selecting my Answer, Divinedar 
John_Ru (rep: 6142) Apr 18, '23 at 1:35 am
Add to Discussion


Answer the Question

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