VBA Userform

kakiebos

Board Regular
Joined
Jun 10, 2018
Messages
62
Hi All,

With a UserForm TextBox you can limit the number of characters to be used in the TextBox properties MaxLength behavior.

I want to know if your set the MultiLine to True, can you limit the number of lines that the user can enter.

For example I have a UserForm with a TextBox. I want the user to be able to enter text into a new line, but there is not supposed to be more than 4 lines of text.

Line 1 Text Good
Line 2 Text Good
Line 3 Text Good
Line 4 Text Good
Line 5 Text Will not be visible in the spreadsheet and therefor not Good.

If there is no way to limit the number of lines in the properties, is there any other way to prevent the user of going over the line limit.

For this example, it will be easy for the user to see if he is using more than the limit, but if the limit is perhaps 30 lines, I still need to be able to prevent the user of going over.

Thanks for your assistance.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
You could put code like this in the userform's code module

Code:
Dim preText As String, preStart As Long, preLength As Long

Private Sub TextBox1_Change()
    With TextBox1
        If .LineCount > 3 Then
            .Text = preText
            .SelStart = preStart
            .SelLength = preLength
            Beep
        End If
    End With
End Sub

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    With TextBox1
        preText = .Text
        preStart = .SelStart
        preLength = .SelLength
    End With
End Sub
 
Upvote 0
Try something like this:

Code:
Private Sub TextBox1_Change()
    If UBound(Split(TextBox1, vbLf)) = 4 Then
    TextBox1 = Left(TextBox1, Len(TextBox1) - 2)
    End If
End Sub

but it only work if you type the data in the textbox, it won't work if the data is copy-pasted.
 
Upvote 0
Sorry, I was wrong, it should be about line not paragraph.
So the code in post 3 won't work.:confused:
 
Upvote 0
This is working like a charm.

Thanks

You could put code like this in the userform's code module

Code:
Dim preText As String, preStart As Long, preLength As Long

Private Sub TextBox1_Change()
    With TextBox1
        If .LineCount > 3 Then
            .Text = preText
            .SelStart = preStart
            .SelLength = preLength
            Beep
        End If
    End With
End Sub

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    With TextBox1
        preText = .Text
        preStart = .SelStart
        preLength = .SelLength
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,415
Messages
6,119,377
Members
448,888
Latest member
Arle8907

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