excelvbatextboxdouble

Reading doubles (numbers) in and out of Excel cells


I'm reading in values from an Excel sheet and have the user checking those numbers/text and hitting save again if they are correct. It works well for any text and numbers, except for my discount being of double type.

Private Sub UserForm_Initialize()

' filling form
TextHN.Text = Range("D3")
'...
' original: TextDP.Text = Range("D7") - but wouldn't read in 12.5
' therefore tried below as well
 Dim new_value As Double
    If IsNumeric(Range("D7").Value) Then
        new_value = CDbl(Range("D7").Value)
        MsgBox new_value # displays 12.5
        Else
        new_value = 1
    End If
    TextDP.Text = CStr(CDbl(new_value))
    MsgBox TextDP.Text # displays 12

I read several articles (Convert TextBox.Value to Double into VBA (Excel 2013) & https://www.mrexcel.com/board/threads/textbox-value-to-double.1066876/), but nothing helped. How do I need to change my code to display my discount in TextDP? (I can read it out as double into Excel file fine)

Update with screenshots:

Excel table UserForm

I'm surprised that it displays it for hospital name and abbreviation correctly (and saves it accordingly), but not for discount percentage and expiry length...


Solution

  • Evidently, SpinButton's or else code affects TextBox's content.

    Highly likely, the TextDP_Change event is handled to setup related SpinButton. Then SpinButton_Change sets TextDP back.