Cdbl crashing when it shouldn't

JeffCorkern

New Member
Joined
Jul 8, 2012
Messages
14
The following statement crashes with a "Type mismatch" error in a userform event-handler:

CDbl(DOTextbox.Value)

I can't find out why it's crashing. Msgbox TypeName(DOTextbox.Value) returns "String." MsgBox CDbl(DOTextbox.Value) also crashes with a "Type mismatch" error. The general help just says CDbl(expression) but doesn't specify what the "expression" has to be. It also doesn't list what error CDbl returns when it's unable to convert "expression".

(Excel 2010 VBA no longer has highly specific context-specific help and simply kicks you to a Web page where you wander around a lot and maybe you find the answer but mostly you don't. I find this EXTREMELY aggravating.)

Here's the code, for what it's worth. The CStr is me trying to beat VBA into submission. It's not working. It's crashing on the Select Case Statement.

Private Sub OKButton_Click()


Select Case CDbl(CStr(DOTextBox.Value)) <> 0 And CDbl(CStr(TemperatureTextBox.Value)) <> 0


Case True
ActiveCell.Value = CDbl(DOTextBox.Value)
ActiveCell.Offset(0, 3).Value = ActiveCell.Offset(0, 3).Value & " " & CDbl(TemperatureTextBox.Value) & "C"
Case False
If CDec(DOTextBox.Value) = 0 Then
MsgBox DOTextBox.Value & " is not a number. Please enter a number."
DOTextBox.SetFocus
End If


If CDec(TemperatureTextBox.Value) = 0 Then
MsgBox TemperatureTextBox.Value & " is not a number. Please enter a number."
TemperatureTextBox.SetFocus
End If
End Select


End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
DOTextbox.Value
What is an example string where it gives you the error?

(Excel 2010 VBA no longer has highly specific context-specific help and simply kicks you to a Web page where you wander around a lot and maybe you find the answer but mostly you don't. I find this EXTREMELY aggravating.)
You can change to the Offline version - in the bottom right corner of the help box change the state to 'Show content only from this computer'.
 
Upvote 0
The light begins to dawn.

It crashes when I feed it a non-convertible string. I feed it "8.38", everything works just fine. I feed it "fred", it crashes. This is because CDbl is returning an error code instead of the string CDbl has to have. Geez, if I had context-specific help, it would tell me what error code is coming back when it crashes.

There is offline help, but it's still not context-specific.
 
Upvote 0
You can test first:

Code:
Private Sub OKButton_Click()
    Dim dDO         As Double
    Dim dTmp        As Double

    If IsNumeric(DOTextBox.Value) Then
        dDO = CDbl(DOTextBox.Value)
    Else
        DOTextBox.SetFocus
        MsgBox "Oops #1!"
        Exit Sub
    End If

    If IsNumeric(TemperatureTextBox.Value) Then
        dTmp = CDbl(TemperatureTextBox.Value)
    Else
        TemperatureTextBox.SetFocus
        MsgBox "Oops #2!"
        Exit Sub
    End If
    
    ActiveCell.Value = dDO
    ActiveCell.Offset(0, 3).Value = ActiveCell.Offset(0, 3).Value & " " & dTmp & "C"
End Sub
 
Upvote 0
I think Val is not international-aware, if the OP cares.
 
Upvote 0
"IsNumeric" is the Magic Function I needed. I had a routing using "Val", but I didn't like that because entering 0 would've caused an error message, although it is actually a number.

This routine does what I want:

Private Sub OKButton_Click()


Select Case IsNumeric(DOTextBox.Value) And IsNumeric(TemperatureTextBox.Value)


Case True
ActiveCell.Value = Val(DOTextBox.Value)
ActiveCell.Offset(0, 3).Value = ActiveCell.Offset(0, 3).Value & " " & Val(TemperatureTextBox.Value) & "C"
ActiveCell.Offset(1, 0).Select
Case False
If Not (IsNumeric(DOTextBox.Value)) Then
MsgBox DOTextBox.Value & " is not a number. Please enter a number."
DOTextBox.SetFocus
End If


If Not (IsNumeric(TemperatureTextBox.Value)) Then
MsgBox TemperatureTextBox.Value & " is not a number. Please enter a number."
TemperatureTextBox.SetFocus
End If
End Select


'clear values
DOTextBox.Value = ""
TemperatureTextBox.Value = ""
DOTextBox.SetFocus


End Sub

Just FYI, I am replacing manual data entry---writing Dissolved Oxygen (DO) concentration and sample temperature on paper(how very last-millennium)---with electronic data entry into an old tablet PC that can be carried around the lab. Much faster and more efficient.

Thanks for the help, people.
 
Upvote 0

Forum statistics

Threads
1,215,093
Messages
6,123,067
Members
449,090
Latest member
fragment

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