Convert TextBox.Value to Double into VBA (Excel 2013)

18,325

CDbl expects already a number but if the textbox is empty then TextBox6.Value is an empty string. CDbl can't cast an empty string into a double.

You can validate if the textbox is a numeric value first to avoid this

If IsNumeric(TextBox6.Value) Then
    new_value = CDbl(TextBox6.Value)
Else
    new_value = 0
End If

Alternatively the Val() function might be an option for you.

new_value = Val(TextBox6.Value)
Share:
18,325
4est
Author by

4est

Updated on July 31, 2022

Comments

  • 4est
    4est over 1 year

    I have TextBox into my form, where user can input a value. In VBA I need to convert the value from string to double.

    I'm doing it like:

    Private Sub UserForm_Initialize()
    
        '....some code
        Dim new_value As Double
        new_value = CDbl(TextBox6.Value)
    
    End sub
    

    But I'm getting the error below:

    enter image description here