最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Excel VBA Userform TextBox content conversion to double returns Type Mismatch error - Stack Overflow

programmeradmin0浏览0评论

I develop a small script for my work in Excel 365's VBA. The basic idea is a Userform where the users can select the desired excel export file from their work and import it to a database. To make things editable I included many TextBoxes where the data is imported from the selected workbook and may be edited before writing it to the database excel workbook.

Everything worked fine until today but now I get "Run-time error '13': Type mismatch" when trying to run my userform. The problem emerges when trying to convert the string/variant type input from the textboxes to double with the CDbl() function.

The input of the textboxes are numbers with periods as decimal separators as shown on the first picture.first pic: the input in the textbox When examining them with Watches, they show up as expected and as they did always (second picture).second pic: the variables as watches

The Debug button takes me to this line of code:

Private Sub Btn_finish_Click()
    
    Dim i%, row%
    Dim teszt As Double
    
    row = Me.Controls("Box_insert_place").Value
    
    If Me.Controls("CheckBox_check").Value = True Then
        'std measurement data'
        If Me.Controls("Box_std_OD_8").Value <> "" Then Cells(row, 16).Value = CDbl(Me.Controls("Box_std_OD_8").Value)
        If Me.Controls("Box_std_OD_1").Value <> "" Then Cells(row, 17).Value = CDbl(Me.Controls("Box_std_OD_1").Value)

    (many more lines here)

    End If

The Error occurs on this part: third picthird pic: the error occurence

This part checks whether the boxes are empty or not to avoid trying to write the contents of an empty box into the database. As the database is used to make trend calculations the datatype must be adjusted, thus I apply CDbl() on it. It worked fine until today: this is a test file I am trying to read and this was the exact same test file I used in all the tests I conducted previously where the whole code worked.

I tried writing the contents of the textbox to a variable while converting to Double like this:

test = CDbl(Me.Controls("Box_std_OD_8").Value)

Got the same error today even though yesterday this method also worked fine.

Tried saving the project, closing it and reopening. Sadly didn't help.

Tried restarting the PC, no result.

In the meantime no updates were performed to the PC or Excel I know of.


Solution: See Rory's comment for primary cause of the problem.

Also check out this thread for further information: Changing decimal separator in VBA (not only in Excel)

发布评论

评论列表(0)

  1. 暂无评论