Set a textbox line-limit..possible?

MGhell

Board Regular
Joined
Jan 16, 2006
Messages
85
Hi,

anyone knows if it's possible to set a limit on the number of lines which can be entered in a userform textbox, instead of setting a limit on the number of characters?

Thanks

Max
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
...I couldn't find a setting which prevents the user from doing as many LF as he wants. I want to somehow "lock" the height of the textbox, so that the number of lines which can be entered in a textbox is limited by the size of the textbox.

Anyone knows how to do this? There must be something....

Max
 
Upvote 0
You could try.

If Not IsNumeric(.Text) Or Len(.Text) <> # Then
MsgBox "Message exceeds maximum # of characters (put in max characters "


Where # is put in how many characters fill the size of the box you want
 
Upvote 0
In the userform module, modify for textbox name.


Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If TextBox1.LineCount > 1 Then
KeyAscii = 0
MsgBox "Only 1 line is permitted", 48, "One line only."
Exit Sub
End If
End Sub
 
Upvote 0
Not sure if this is what you wanted..
Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim myLimit As Integer, x, txt As String, i As Integer

myLimit = 5
With Me.TextBox1
     If Len(.Text) = 0 Then Exit Sub
     x = Split(.Text, vbLf)
     If UBound(x) <= myLimit -1 Then Exit Sub
     MsgBox "Allowed only " & myLimit & " lines in this box"
     For i = 0 To myLimit -1
         txt = txt & x(i) & vbLf
     Next
     .Text = Left(txt, Len(txt) - 1)
End With

End Sub
 
Upvote 0
I was wondering if there is a way to set a character limit per cell and overflow remaining characters to the next cell. Example: I need to set a cell to accept no more than 59 characters then overflow remaining characters to the next cell.
 
Upvote 0
In the userform module, modify for textbox name.

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If TextBox1.LineCount > 1 Then
KeyAscii = 0
MsgBox "Only 1 line is permitted", 48, "One line only."
Exit Sub
End If
End Sub
For one line only... couldn't you just set the MultiLine property to False and eliminate the above code altogether?
 
Upvote 0
In the userform module, modify for textbox name.


Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If TextBox1.LineCount > 1 Then
KeyAscii = 0
MsgBox "Only 1 line is permitted", 48, "One line only."
Exit Sub
End If
End Sub
Hi Tom,

I used your code with my modifications for only 6 lines in my ActiveX Text Box called CustomerComments, and my question is; when I reach line 6, it allows me to hit "enter" which then advances to the next line when the "Only 6 lines are permitted" message appears, but now I have lost the 1st line in the text box.

Also, what does this part of your code do: 48, "One line only.?

Private Sub CustomerComments_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If CustomerComments.LineCount > 6 Then
KeyAscii = 0
MsgBox "Only 6 lines are permitted", 48, "One line only."
Exit Sub
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,848
Messages
6,162,419
Members
451,765
Latest member
craigvan888

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