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

Textbox Overflow Error

0

I'm getting an OVERFLOW error and I believe because I'm using the same textbox txtTOTALcst.Value in more than one calculation. I get the error soon as I try to put a value in the txtQTY textbox. I put in bold where the error highlights.

Before this code activates the txtTOTALcst textbox is already populated with a value.

Private Sub txtTOTALcst_Change()
    If txtTOTALcst.Value = "" Then txtTOTALcst.Value = 0
    If txtMarkup.Value = "" Then txtMarkup.Value = 0
    txtNegAmt.Value = (CDbl(txtTOTALcst.Value) / CDbl(txtMarkup.Value)) * 100 
End Sub
Private Sub txtMarkup_Change()
    
    If txtTOTALcst.Value = "" Then txtTOTALcst.Value = 0
    If txtMarkup.Value = "" Then txtMarkup.Value = 0
    txtNegAmt.Value = (CDbl(txtTOTALcst.Value) / CDbl(txtMarkup.Value)) * 100
    
    sh.Cells(iRow + 1, 11) = txtMarkup.Value
End Sub


Private Sub txtNegAmt_Change()
sh.Cells(iRow + 1, 12) = txtNegAmt.Value
End Sub

So how do I tell the code that the txtTOTALcst  already has a value?

Or is there another way I can calculate the negotiation amount?

Answer
Discuss

Answers

0
Selected Answer

Dividenar

The overflow error arises since your code says:

If txtMarkup.Value = "" Then txtMarkup.Value = 0

so your following line:

txtNegAmt.Value = (CDbl(txtTOTALcst.Value) / CDbl(txtMarkup.Value) * 100

is a "divide by zero" calculation which overflows the maximum value of a Double variable or any variable in fact, since it's infinity! 

I also think your logic is wrong in that the first line:

'If Not txtTOTALcst.Value = "" Then txtTOTALcst.Value = 0

since it always sets  txtTOTALcst to zero (and furthermore 0/0 calculations are indeterminate!).

In the attached revised file, I've:

  1. commented out/ replaced that line and
  2. corrected your Negotiation $ calculation and
  3. rounded up the resultant Negotiaton value to 2 decimal places for currency (though you may need first to change the end of that line from the form "###.00" to "###,00" for your locale). You might want to do similar for total cost.

See changes in bold:

Private Sub txtTOTALcst_Change()
    'If Not txtTOTALcst.Value = "" Then txtTOTALcst.Value = 0
    If txtTOTALcst.Value = "" Then txtTOTALcst.Value = 0
    ' make this a default value
    If txtMarkup.Value = "" Then txtMarkup.Value = 10
    'txtNegAmt.Value = (CDbl(txtTOTALcst.Value) / CDbl(txtMarkup.Value) * 100)
    txtNegAmt.Value = (CDbl(txtTOTALcst.Value) * CDbl(txtMarkup.Value)) / 100
    'format that to 2 decimal places (change decimal marker if needed)
    txtNegAmt.Value = Format(WorksheetFunction.RoundUp(txtNegAmt.Value, 2), "###.00")
End Sub

Private Sub txtMarkup_Change()
    'If Not txtTOTALcst.Value = "" Then txtTOTALcst.Value = 0
    If txtTOTALcst.Value = "" Then txtTOTALcst.Value = 0
    ' make this a default value
    If txtMarkup.Value = "" Then txtMarkup.Value = 10
    txtNegAmt.Value = (CDbl(txtTOTALcst.Value) * CDbl(txtMarkup.Value)) / 100
    'format that to 2 decimal places (change decimal marker if needed)
    txtNegAmt.Value = Format(WorksheetFunction.RoundUp(txtNegAmt.Value, 2), "###.00")
    sh.Cells(iRow + 1, 11) = txtMarkup.Value
End Sub

Alternatively, you might use MROUND to round to the nearest 5 cents (say, =0.05), using:

txtNegAmt.Value = Format(WorksheetFunction.MRound(txtNegAmt.Value, 0.05), "###.00")

(again adjusting the decimal separator as necessary).

Hope this works for you (now you can easily add cost and negotiation value to get Selling Price).

Discuss

Discussion

Thank you perfect.  Much appreciated.
Divinedar (rep: 20) Mar 14, '23 at 12:44 pm
Don't call me perfect! :) Haha! 

Glad that worked. Thanks for selecting my Answer, Divinedar. 
John_Ru (rep: 6142) Mar 14, '23 at 12:52 pm
Add to Discussion


Answer the Question

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