You are here:

Excel/vba userform output


QUESTION: I have a userform that numeric data is input into. I am exporting that data to the spreadsheet with the following code
Range("xyz").value = Textbox1. I want the exported data to be formatted as a number but it comes out as text. How can I solve this.

Thank you

ANSWER: Philip Hogan,

Try this

Range("xyz").value = (Textbox1.value) * 1


Range("xyz").value = cdbl(Textbox1.value)

This assumes the textbox1 will have value in it.

Tom Ogilvy

---------- FOLLOW-UP ----------

QUESTION: I used your first method and it works fine if there is a number in the textbook, but I have about 70 textboxes and often the user would not be using all of them so the macro fails. How do I get around this?

Thank you again for your time and help. Much appreciated

ANSWER: Philip,

Depends on what you want to appear in the cell.

if len(trim(Textbox1.Value)) = 0 then  ' empty textbox
elseif isnumeric(Textbox1.Value) then    ' number in textbox
  Range("xyz").Value = (Textbox1.value) * 1
  Range("xyz").Value = Textbox1.Value    ' something else in textbox
End if

Tom Ogilvy

---------- FOLLOW-UP ----------

QUESTION: Since I have about 70 textboxs to do this to, can you show me how to make to loop that would change the textbox number and also changing the name of the xyz range to xyz1 would be where textbox 1 would be copies to.

Finally, you said in your last reply depends what you want to appear in the cell. I want to be able to use the output cell in a formula, so I don't want either a blank cell or a cell with 0 in it this way I can add that cell in a formula and not get an error. .

Again many thanks


If you are just going to run a macro to write the information to the cells, then you could do it like this

Private Sub CommandButton1_Click()
Dim i As Long, sTb As String, sRange As String
Dim r As Range, tb As msforms.TextBox
For i = 1 To 70
 sTb = "Textbox" & i
 sRng = "xyz" & i
 Set r = Range(sRng)
 Set tb = Me.Controls(sTb)
 On Error Resume Next
 If Len(Trim(tb.Value)) <> 0 Then
   r.Value = Trim(tb.Value) * 1
 End If
 On Error GoTo 0
End Sub

so this confuses me:
>I want to be able to use the output cell in a formula, so I don't want either a blank cell or a cell with 0 in it

If you use the sum formula then blanks and zero cause no problem.

If you use the plus operatator   =A1+B1+C1 as an example, then that would have problems with a space or nonnumeric character in the cell.

But if your textbox is empty, what do you want the code to do?  

Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Tom Ogilvy


Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]


Extensive experience.

Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 All rights reserved.