Free Microsoft Excel 2013 Quick Reference

Limit Userform TextBox Characters


I am using Excel 2000.

I have userform which contains a text box, I would like to limit the number of characters entered into the text box.

For example
I would like users to only be able to enter 25 characters into txtName, when they try and enter the 26th character I don't want it to appear in the text box.

Any ideas would be great

Post your answer or comment

comments powered by Disqus
Hi, I've spent quite a while trying to limit the user's input to a userform textbox to no avail. For example one textbox on the form should only be numbers and I therefore want to restrict the user to typing in a two digit code like 02 or 72 (not for calculations). Another textbox I only want to allow the user to input 6 characters in the format letter, four numbers and a letter. If the user inputs the wrong stuff a message box pops up and the focus is reset (I can do that bit!).

Any help would be appreciated.

Hello OzGrid Family,

I am very new to this forum and VBA. I would love your help. I would like to place data from a userform textbox into the first empty cell within the range B5:B30. When I think about it algorithmically, the steps I would like to do are:

1. Find the first empty cell in range B5:B30
2. Paste the string from a user form Textbox into that cell
3. Then exit that function

Any assistance or suggestions in how I can achieve this in VB coding?

Thanks in advance for your help.

What I'm looking to do is compare a userform textbox string (user entry, executed by another event) to, say, a range in Column D of Sheet1.

If there's a match, return a true. Conversely, return a false. (Reason: To prevent duplicates). Other code would follow based on the result.

If there's a way, I've not been able to ascertain it on & off again since, but would like resolution. I've found nothing helpful in hours of searching & trial/error.

Thanks in advance for help.


I'm building a UserForm that has several TextBoxes to gather basic project information to fill in the header of a report...

The first TextBox is the Job #... I'm trying to implement code for an "Auto Fill" button (based on working VBA code from another workbook) that will fill the other TextBoxes in the UserForm...
The working VBA code from the other workbook does "Auto Fill" for Worksheet Ranges, not UserForm TextBoxes...

How should I reference the UserForm Textboxes in the code to specify their value?

Sample of experimental code, not working;

Workbooks("Template Generator.xls").Forms("ProjectInfo").Activate 
If TextBox("InptJobNum").Text = "" Then 
    MsgBox "Please Insert Job Number" 
    Exit Sub 
    Jnum = TextBox("InptJobNum") 'Specify Job Number
    Workbooks("Job Log.xls").Sheets("Job Log").Activate 
End If 
If WorksheetFunction.CountIf(Range("A2:A1000"), Jnum) = 0 Then 
    Workbooks("Template Generator.xls").Forms("ProjectInfo").Activate 
    MsgBox "Invalid Job Number, or cannot be located... Please correct and try again" 
    Exit Sub 
End If 

If you like these VB formatting tags please consider sponsoring the author in support of injured Royal Marines

i have a userform listbox, when the selection is double clicked it places the data in a userform textbox.
eg customer name.
depending on which customer is selected i need the customer details which are stored on a sheet to appear in the other userform textbox's.

please check out the workbook

I have a userform where I want a textbox to show a value already entered in my worksheet.

This works fine, however, I have set the formating of the cell in my worksheet to 2 decimal places. When my text box looks at this value it shows it in full and also doesnt not round up.

For example:

cell value = 0.51

When showing in the userform textbox it shows 0.509999999999999999

How can I make my textbox show exactly what is showing in my cell.

Hope this makes sence


This a new question that arised from my earlier posting in

I have the following working code below, but am having trouble finding coding examples to select specific cells from the selected row (that was found by selecting a ComboBox value)and update TextBoxes with those individual values after the UserForm has been initialized (the bold "GREEN" comment in the code below). I have been able to find plenty of references to update TextBox values to Cells, but that doesn't do me much good in this application since the User needs to verify the old data in these cells before updating them using the UserForm TextBoxes.

I was toying around with several different variations of code (none of which worked properly), so I left it out for clarity of my working code. I'll post up this non-working code as needed, because I really wanted a fresh answer...not what I was trying to do. The attached file should be sufficient to see what's going on, but if not please ask questions.

thanks, ~Dan

     'Add list entries to the combo boxes. The value of each
     'box matches the existing SR Information spreadsheet entries
    With Sheets("SR Information") 
        .Range("A2", .Cells(Rows.Count, "A").End(xlUp)).Name = "MyRange" 
    End With 
    SRnumber.RowSource = "MyRange" 
     ' Set initial values to TextBoxes -not needed, just for testing-
     '    NBdate.Text = "Test NBD SR Number"
     '    ApartNumber.Text = "Test AP SR Number"
     '    UPdetails.Text = "Test UD SR Number"
End Sub 
Private Sub SRnumber_Change() 
    Dim ServiceRequestNumber As String 
    Dim c As Range 
    Dim rngG As Range 
    Sheets("SR Information").Select 
    With Selection 
         'Find value in ComboBox, and select row
        ServiceRequestNumber = SRnumber.Value 
        For Each c In Intersect(ActiveSheet.UsedRange, Columns("a")) 
            If c = ServiceRequestNumber Then 
                If rngG Is Nothing Then Set rngG = c.EntireRow 
                Set rngG = Union(rngG, c.EntireRow) 
            End If 
        Next c 
        [B][COLOR="Red"] ' >Insert Code Here?<  Update other TextBoxes with information contained in specific cells on the
selected row[/COLOR][/B]
    End With 
End Sub 

If you like these VB formatting tags please consider sponsoring the author in support of injured Royal Marines


I have spreadsheet in which col B has validation Rule
Now I m inserting the values in this col(B) through userform textbox but it is ignoring the validation rule.

Is there any possible solution to do this.

Good Morning all,

I have a question regarding "copying data entered in a userform to cells in worksheets." The case is following;

I have 2 userforms and the passage between those userforms are made with a next/back button, I ask some general information such as customer name, location etc.. and when user clicks at the end to the "nextbutton" I want all the data entered in the userform/textboxes to be written in defined cells

Private Sub Next1_Click()
If Customer.Text = "" Then
MsgBox "Please complete Customer Field"
Exit Sub
ElseIf Countrychoose.Text = "" Then
MsgBox "Please choose a Country from the list"
Exit Sub
ElseIf Contact.Text = "" Then
MsgBox "Please enter a Contact"
Exit Sub
ElseIf Dollar.Value = False Then
If Euro.Value = False Then
MsgBox "Please choose a Currency"
Exit Sub
ElseIf Output.Value = False Then
If Time.Value = False Then
MsgBox "Please choose a Constant"
ElseIf Application.Text = "" Then
Application.Text = "N/A"
End If
End If
End If
End Sub

Sub Copydata()
Sheets("Worksheet").Range("D13") = Customer_Info.Customer.Text
Sheets("Worksheet").Range("D17") = Contact.Text
Sheets("Worksheet").Range("D15") = Countrychoose.Text
Sheets("Worksheet").Range("D19") = Contact.Text
End Sub

I have a userform with several text boxes onit that display the contents of cells on the sheet. The cells contain a calculated value and I have formatted them to display ~ 0 ~ decimal places. They appear properly in the cells on the sheet.

In the userform textboxes however, they display decimal places as calculated; this is irritating as the textboxes are not sized to accomodate cases where the number calculates out to several decimal places.

Any suggestions?


I need to test the value of several userform textboxes and I'm currently using the code Code:
 where Ctrl is declared as a Control.

So far, so good. However, I've noticed that the order in which the controls are tested doesn't correspond with my tab order (suspect it may be the order they were created).

Other than re-creating my userform in the order I wish the controls to be tested, is there a method for specifying the order they should be tested?

Many thanks

Validating UserForm TextBox to Only Accept Text

Validating UserForm TextBox to Only Accept Numbers If you are fairly comfortable with Excel VBA you will most likely want to design a UserForm to ensure correct use of your Spreadsheet. However, just placing some TextBoxes on a UserForm for users to enter data is not enough. For example, the TextBoxes may be intended to take only numeric data and not Text strings.

Well, here I am, back again, with yet another barrier to breach. I am having a lot of trouble being able to print the contents of a userform textbox. I had seen reference to this on the excel.programming newsgroup that used Windows API calls to carry out this function. Unfortunately, the sample code supplied was for Excel 97, and I am cross-building this application using both Excel 2000 and 2002. I also want it set so that I can set the criteria for what to print (for example, I might want to print all log entries between two dates or just the log entries generated by a certain user.

For that matter, is it possible to set criteria when loading the contents of a file into a textbox?

As always, any help that can be rendered would be greatly appreciated.

Is the filename length in Excel (office XP) limited to 92 characters? I have
found this limitation today and do not no whether it is a bug or not.

I need a userform textbox event that fires after I tab or click out of the textbox. Going by the list of options:Beforedragover, BeforeDroporPaste, Change, DblClick, DropButtonClick, Error, Keydown, Keypress, keyup, mousedown, mousemove, mouseup.

I can't figure out which one will do what I want. The change event happens instantaneously which doesn't work. I need to fire off the event when my focus leaves the textbox.

How can i set a userform textbox properties so i can enter HH:MM.?


I would like to be able to copy only the selected userform.textbox.text to another userform.textbox.
Is this possible with the use of a button that resides on the same userform as the two textboxes. or better yet Being able to use right-click to copy information within a userform textbox then able to right-click in another userform text box and past the data.

Cheers for any info that may help.

Hi All,

Is it possible to display random text from a range of cells in a
userform textbox when the userform is activated?

Also if the range refers to non contiguous cells.


I have a userform textbox that has a value in it. Is there a way to set focus on this textbox with the current value being selected? I would like the user to be able to replace the current value immediately instead of having to first delete the current value. Thank you

Hi Folks,
I've been trying to get the text in my VBA Userform textboxes to wrap onto the next line when it runs into the border of the box.
How do you set it up so that it does this. I've tried the properties window and set the word wrap to true but the text still stays on one line and goes out of sight.
Any help is appreciated.

EDIT: Thanks Marvin, it worked perfectly!


Is there a limit to the number of characters allowed in a cell (or is it
just a limit to the size of a formula)

I have a userform which has a textbox for collecting information. The user
types a message (some of which can be quite long.)
The code runs and does various things including transfering the text from
the textbox into a cell on the worksheet.

Will I have any limitations?

Win 2000 and Excel97

I am working in Office 2000, and have 3 Textboxes, I need to copy text from Textbox1 and Textbox2 to Textbox3, and the code below works but only when the character length is limited. I need to be able to show much more text. Suggestions. I am trying some of the code

Sub Text_Copy() 
    Dim ch1 As Characters 
    Dim ch2 As Characters 
    Dim ch3 As Characters 
    With Sheets("Aim Up Description") 
        Set ch1 = .Shapes("TextBox1").TextFrame.Characters 
        Set ch2 = .Shapes("TextBox2").TextFrame.Characters 
    End With 
    With Sheets("Description Summary") 
        Set ch3 = .Shapes("TextBox3").TextFrame.Characters 
    End With 
    ch3.Text = ch1.Text & " " & ch2.Text 
End Sub 

If you like these VB formatting tags please consider sponsoring the author in support of injured Royal Marines
I have found code on other posts that I have tried (like, Range("A16").Value = Val("TextBox1"), but I am still to new at this and can't get it to work. Any suggestions are greatly appreciated.


The following worked for me. One thing to note is that, their would be less
of an issue if you used a textbox from the Control toolbox.

Private Sub CommandButton1_Click()
Set allText = UserForm1.TextBox1
Set txtBox2 = ActiveSheet.DrawingObjects(1)
For x = 1 To Len(allText) Step 250
theText = Mid(allText, x, 250)
txtBox2.Characters(Start:=x, Length:=250).Text = theText
End Sub

You'll neet to change the numbers to suit your sheet and userform.

Dan E

"John Wilson" > wrote in message
> Repost.......
> Hit a brick wall with this one and I'm sure it's possible (just not
> for me at the moment).
> Code is directly from the KB article for copying text between
> textboxes using the character method. The problem is that my
> "CopyFrom" textbox is on a UserForm and the "CopyTo" textbox
> is on the Active sheet.
> UserForm is named "Amendment"
> The textbox on the UserForm is TextBox2
> The TextBox on the Activesheet is TextBox22
> What I'm trying to do is copy the text from the TextBox on the UserForm
> to a TextBox on the Activesheet. A straight copy/paste works if the text
> is under 256 characters but if it's over that, the TextBox on the Active
> sheet
> comes up blank.
> Coding below:
> Sub TextBox_To_TextBox()
> Dim x As Integer
> Dim txtBox1 As TextBox, txtBox2 As TextBox
> Dim theText As String
> Set txtBox1 = ActiveSheet.DrawingObjects(1)
> ' Above should reference UserForm "Amendment", "TextBox2"
> Set txtBox2 = ActiveSheet.DrawingObjects(2)
> ' Above should reference Activesheet(Textbox22)
> For x = 1 To txtBox1.Characters.Count Step 250
> theText = txtBox1.Characters(start:=x, Length:=250).Text
> txtBox2.Characters(start:=x, Length:=250).Text = theText
> Next
> End Sub
> Any help with this would be certainly appreciated.
> Thanks,
> John

No luck finding an answer? You could always try Google.