Need help; trying to figure out why I am getting an error with a leading decimal point in field on userform

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
476
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
It only trips an error when I "tab" into the field (upon which it turns blue) and then if a decimal point is entered as the first character I get:

"Run-time error '13:'
Type mismatch"

If I use the backspace key to remove any numerals that might be in the field, and then hit decimal (but its still the first character entered) then I do not get an error and it accepts the leading decimal.. (???)
(the RED code is where the error is highlighted)
Code:
[COLOR=#008000]' CODE FOR THE FIRST TEXT BOX FOR ALLOWING A DECIMAL POINT AS THE LEADING CHARACTER:[/COLOR]
Private Sub txtCostProd1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Not (KeyAscii > 47 And KeyAscii < 59) Then
  If KeyAscii = 46 And Len(txtCostProd1.Text) = 0 Then
    txtCostProd1.Text = "0"
  ElseIf Not (KeyAscii = 46 And InStr(txtCostProd1.Text, ".") = 0) Then
Beep:     KeyAscii = 0
  End If
End If
End Sub

[COLOR=#008000]' CODE FOR THE SECOND TEXT BOX FOR ALLOWING A DECIMAL POINT AS THE LEADING CHARACTER:[/COLOR]
Private Sub txtCostShip1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Not (KeyAscii > 47 And KeyAscii < 59) Then
  If KeyAscii = 46 And Len(txtCostShip1.Text) = 0 Then
    txtCostShip1.Text = "0"
  ElseIf Not (KeyAscii = 46 And InStr(txtCostShip1.Text, ".") = 0) Then
Beep:     KeyAscii = 0
  End If
End If
End Sub

Private Sub txtCostProd1_Change()
    TextBoxesSum
End Sub

Private Sub txtCostShip1_Change()
    TextBoxesSum
End Sub

Private Sub TextBoxesSum()
    Dim Total As Double
    Total = 0
        
    If Len(txtCostProd1.Value) > 0 Then Total = [COLOR=#ff0000][B]Total + CDbl(txtCostProd1.Value)[/B][/COLOR]
    If Len(txtCostShip1.Value) > 0 Then Total = Total + CDbl(txtCostShip1.Value)
    
    txtCost1.Value = Total

End Sub

Here is a screenshot of my form (part of it) showing the textboxes as mentioned in the above code:
25p2req.jpg


Everything else in the code functions as intended (it sums up the totals just as it should), but I cant understand why it doesnt like it when a decimal point is entered as the first character, and only when the number that is present in the textbox is BLUE(as in highlighted) If I backspace over the character(s) that are in there, then hit the decimal point, it accepts it. (?) very strange.... :(
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I'm guessing since you are making the entry in a text box and the variable is delcared as Double, that the compiler is somehow picking that decimal up as txt and throwing the error 13. Try starting your entry with a zero and see if it still throws the error.
 
Upvote 0
The problem is in this part of your code line...

CDbl(txtCostProd1.Value)

When you enter a decimal point as the first character in the textbox, that part of your code line becomes this..

CDbl(".")

A decimal point by itself (which could be a "period" of course) cannot be converted to a Double as it is not a number. If you change that part of your code to this, I think it will work for you...

CDbl("0" & txtCostProd1.Value)

Alternately, you should be able to use the Val function instead of the CDbl function...

Val(txtCostProd1.Value)
 
Last edited:
Upvote 0
Thank you, Rick! You came through again for me. Much appreciation to you kind sir.
icon14.png


The problem is in this part of your code line...

CDbl(txtCostProd1.Value)

When you enter a decimal point as the first character in the textbox, that part of your code line becomes this..

CDbl(".")

A decimal point by itself (which could be a "period" of course) cannot be converted to a Double as it is not a number. If you change that part of your code to this, I think it will work for you...

CDbl("0" & txtCostProd1.Value)

Alternately, you should be able to use the Val function instead of the CDbl function...

Val(txtCostProd1.Value)
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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