Convert a user form TextBox input to numerical value long or date

Kappes105

New Member
Joined
Sep 15, 2021
Messages
11
Office Version
  1. 2016
Platform
  1. Windows
I would like to create an array from the userform input of the TextBoxes. However, I get an error message (Type mismatch (Error 13)) when I want to convert the values from the userform TextBox into the number type clng or cdate
Can someone tell me what I am doing wrong? My array looks like this:
mFields = Array( _
Array(CStr(TextBox1), 2, vbString, "R", 0), _
Array(CStr(TextBox2), 3, vbString, "R", 0), _
Array(CDate(TextBox3), 3, vbString, "R", 0), _
Array(CLng(TextBox4), 4, vbLong, "R", 100))

many thanks!!
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Your code likely to work if values of correct data type will always exist in to your textboxes but if an expression passed to a function is outside the range of the data type to which it is to be converted you will get an error. One way to overcome this but not recommended, you could just wrap your code with On Error Resume Next to ignore the error.

Suggest consider using an appropriate Is function in your code to first determine if the value can be coerced to the required data type and only then apply the type conversion function.

Example

Rich (BB code):
Option Base 1

Private Sub CommandButton1_Click()
    Dim mfields     As Variant
    mfields = Array(Array(CStr(TextBox1), 2, vbString, "R", 0), _
                    Array(CStr(TextBox2), 3, vbString, "R", 0), _
                    Array(TextBox3, 3, vbString, "R", 0), _
                    Array(TextBox4, 4, vbLong, "R", 100))
  
    If IsDate(mfields(3)(1)) Then mfields(3)(1) = CDate(mfields(3)(1))
    If IsNumeric(mfields(4)(1)) Then mfields(4)(1) = CLng(mfields(4)(1))
  
End Sub


Hope Helpful

Dave
 
Last edited:
Upvote 0
I am using separate TextBox class modules and checking the TextBox Events to ensure that the correct values are entered.
The array should help me to store the data correctly in the table. Unfortunately your example does not work
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

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