Avoid consecutive character input into a textbox

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
I have tried to reverse engineer a vba script to produce the code below here which allows for only alphabets and the characters listed.


Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
With TextBox1
Select Case KeyAscii 
             Case Asc("a") To Asc ("z")
             Case Asc("A") To Asc ("Z")
             Case Asc("-")
             Case Asc(".")
             Case Asc(" ")
             Case Else: KeyAscii =0
End Select 
End With 
End Sub

I want to stop repeated characters of "-", "." or " " being entered. That's :
1. Don't allow "--", ".." or " "
2. "A-B-","A.B." or "A B " is allowed.
3. Don't allow "A -" , "A- " or "A - ". That is, if a hyphen follows a space immediately, remove the space. So "A -" becomes "A-".

And if a space immediately follows a hyphen, remove the space.
So "A- " becomes "A-"

4. After a dot, if I press any key other than the space key and there is no space after the dot, then add a space before registering the pressed key. But if the pressed key is the hyphen, no space should be added.

I know this is very complex to me. But I believe some got answers over here.

Thanks for reading.

Regards
Kelly
 
Last edited:
Great! !!

I have tested all the scripts and so far @ Rick Rothstein is the one getting me the real thing I am looking for.

I appreciate all of your effort. I am really learning new skills indeed.
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
One drawback for the code posted so far is that it will allow "forbidden" text to be copy/pasted into the TextBox. The following code will not let the user enter a forbidden character or character sequence and it will not permit forbidden text from being copy/pasted into the TextBox...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Dim LastPosition As Long

Private Sub TextBox1_Change()
  Static LastText As String
  Static SecondTime As Boolean
  If Not SecondTime Then
    With TextBox1
     If .Text Like "*[!A-Za-z. -]*" Or .Text Like "*..*" Or .Text Like "*--*" Or .Text Like "*  *" Then
        Beep
        SecondTime = True
        .Text = LastText
        .SelStart = LastPosition
      Else
        LastText = .Text
      End If
      .Text = Replace(Replace(Replace(.Text, " - ", "-"), "- ", "-"), " -", "-")
    End With
  End If
  SecondTime = False
End Sub

Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  With TextBox1
    LastPosition = .SelStart
    'Place any other MouseDown event code here
  End With
End Sub

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  With TextBox1
    LastPosition = .SelStart
    'Place any other KeyPress checking code here
  End With
End Sub[/TD]
[/TR]
</tbody>[/TABLE]
Can you get these working please?
1. Don't allow space, dot or hyphen when textbox is empty
2. Don't allow dot immediately after a hyphen
3. Don't allow dot immediately after a space
4. After a dot if I press the keys any alphabet, and there is no space in front of the dot, then place a space before registering the character
5. But if the pressed key is hyphen then just register it without adding space.


Regards
Kelly
 
Upvote 0
Can you get these working please?
1. Don't allow space, dot or hyphen when textbox is empty
2. Don't allow dot immediately after a hyphen
3. Don't allow dot immediately after a space
4. After a dot if I press the keys any alphabet, and there is no space in front of the dot, then place a space before registering the character
5. But if the pressed key is hyphen then just register it without adding space.
The following code (which replaces all the code I gave you earlier) implements items #1 , #2 and #3 . I thought my code already did #5 (can you give me an example of text where it doesn't)?
Code:
[table="width: 500"]
[tr]
	[td]Dim LastPosition As Long

Private Sub TextBox1_Change()
  Static LastText As String
  Static SecondTime As Boolean
  If Not SecondTime Then
    With TextBox1
     If .Text Like "*[!A-Za-z. -]*" Or .Text Like "*..*" Or .Text Like "*--*" Or .Text Like "*  *" Or .Text Like "*-.*" Or .Text Like "* .*" Or .Text Like "[. -]" Then
        Beep
        SecondTime = True
        .Text = LastText
        .SelStart = LastPosition
      Else
        LastText = .Text
      End If
      .Text = Replace(Replace(Replace(.Text, " - ", "-"), "- ", "-"), " -", "-")
    End With
  End If
  SecondTime = False
End Sub

Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  With TextBox1
    LastPosition = .SelStart
    'Place any other MouseDown event code here
  End With
End Sub

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  With TextBox1
    LastPosition = .SelStart
    'Place any other KeyPress checking code here
  End With
End Sub[/td]
[/tr]
[/table]
As for #4 , I have a question about it. Did you really mean to say "in front of the dot"? I ask because #4 insures that will never happen. It would seem like you meant to say "after the dot" instead. If that is the case, then if I implement that, there would be no way to place certain abbreviations into the TextBox (for example, like Ph.D which is short for Doctor of Philosophy). Did you still want me to add item #4 to the code above?
 
Last edited:
Upvote 0
@Rick Rothstein,

Yes add the #4 since you have already shown the code with the #4 , I will then decide when of the textboxes to have which code.

I really appreciate your patience
 
Last edited:
Upvote 0
@Rick Rothstein,

Yes add the #4 since you have already shown the code with the #4 , I will then decide when of the textboxes to have which code.
Here is the code that automatically inserts the space between dots and alphabetic characters...
Code:
[table="width: 500"]
[tr]
	[td]Dim LastPosition As Long

Private Sub TextBox1_Change()
  Dim X As Long
  Static LastText As String
  Static SecondTime As Boolean
  If Not SecondTime Then
    With TextBox1
     If .Text Like "*[!A-Za-z. -]*" Or .Text Like "*..*" Or .Text Like "*--*" Or .Text Like "*  *" Or .Text Like "*-.*" Or .Text Like "* .*" Or .Text Like "[. -]" Then
        Beep
        SecondTime = True
        .Text = LastText
        .SelStart = LastPosition
      Else
        LastText = .Text
      End If
      For X = 1 To Len(.Text) - 1
        If Mid(.Text, X, 2) Like ".[A-Za-z]" Then .Text = Application.Replace(.Text, X + 1, 0, " ")
      Next
      .Text = Replace(Replace(Replace(.Text, " - ", "-"), "- ", "-"), " -", "-")
    End With
  End If
  SecondTime = False
End Sub

Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  With TextBox1
    LastPosition = .SelStart
    'Place any other MouseDown event code here
  End With
End Sub

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  With TextBox1
    LastPosition = .SelStart
    'Place any other KeyPress checking code here
  End With
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Great!!!

Very wonderful code. It's working very fine.

Thanks for it
 
Upvote 0

Forum statistics

Threads
1,215,172
Messages
6,123,447
Members
449,100
Latest member
sktz

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