Minimum length in text box

GeorgeWhite

New Member
Joined
Apr 20, 2017
Messages
27
Hello,

I currently have a userform which is shown after a selection on another userform. This userform requires you to type in a number which I have set a maximum length to 8 characters. I am now looking for a way to modify this to have a minimum of 8 characters too. I would like for it that when they select the ok button a message will pop up saying "Minimum of 8 characters only" and then the userform will be presented again and this will loop until they submit at least the minimum/maximum.

This is the current code I am using for the maximum characters:
Code:
Private Sub UserForm_Initialize()
HideTitleBar Me
Me.TextBox1.MaxLength = 8
End Sub

But I also have this code for the command button:
Code:
Private Sub CommandButton2_Click()        
Dim Cert As String
            Cert = Me.TextBox1.Value
        For Each cell In Worksheets("Certificate#").Range("A2:A500")
            If cell.Value = Cert Then
        If MsgBox("Certificate number already in use.", vbInformation) = vbOK Then
             Me.TextBox1.Text = ""
        End If
        Exit Sub
    End If
    Next cell
    Dim lRow As Long
    Dim ws As Worksheet
    Set ws = Worksheets("Certificate#")
    lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    With ws
        .Cells(lRow, 1).Value = Me.TextBox1.Value
    End With
    Me.TextBox1.Value = ""
Unload CertificateNo
End Sub

Many thanks.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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