Code Amendment Needed: Clear the contents of a text when its contents are selected while pressing a key - VBA

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,020
Office Version
  1. 2016
Platform
  1. Windows
Hello good morning/afternoon/evening.
I have this code here that I want to do a few amendments and I need your help with that.

I want to achieve two goals but if I can get at least one goal, I will be very happy.

1. When I select all the contents of the textbox and press a key afterwards, I want to replace the pressed character with the highlighted content - just like it is with text editors.

2. On this line:
Code:
Case Is > 7 : .Text = Left (.Text, 7)
When I press keys after all 8 characters inside the textbox, the last character keeps being replaced with whatever new character or key pressed.

How do I avoid that?
If I want to display alert while there are 8 characters inside already and I press a key again?

Code:
Private Sub TextBox1_KeyPress (ByVal KeyAscii As MSForms.ReturnInteger)
     With TextBox1 
           KeyAscii = KeyAscii * -CLong (Chr (KeyAscii) Like "[0-9]")
           Select Case Len (.Text)
               Case Is = 2 : .Text = .Text & "-"
               Case Is = 5 : .Text = .Text & "-"
               Case Is > 7 : .Text = Left (.Text, 7)
          End Select 
     End With
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,932
Office Version
  1. 2019
Platform
  1. Windows
Hi,
try setting the MaxLengh property of the control and see if that helps you

Rich (BB code):
With TextBox1
            .MaxLength = 8
           KeyAscii = KeyAscii * -CLng(Chr(KeyAscii) Like "[0-9]")
           Select Case Len(.Text)
               Case Is = 2: .Text = .Text & "-"
               Case Is = 5: .Text = .Text & "-"
               Case Is > 7: .Text = Left(.Text, 7)
          End Select
     End With

Dave
 

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,020
Office Version
  1. 2016
Platform
  1. Windows
Hi @dmt32,
I have tried that maxlength before but it did not work.

The last character keeps changing when I press a key.

However, your post helped me to fix that error using the len function on this line:

Code:
  Case Is > 7: .Text = Left(.Text, 7)

Implementation :

Code:
......
  Case Is > 7: 
      If Len (.Text) = 8 Then
         MsgBox "Hi"
       Else
         .Text = Left(.Text, 7)
       End If

Thanks for the tip.
 
Solution

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,020
Office Version
  1. 2016
Platform
  1. Windows
How about the first request?

If anyone has any idea how to handle that situation I would love it.
 

Forum statistics

Threads
1,148,160
Messages
5,745,121
Members
423,926
Latest member
Komaua Ubaroro

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
Top