SamarthSalunkhe
Board Regular
- Joined
- Jun 14, 2021
- Messages
- 103
- Office Version
- 2016
- Platform
- Windows
Hi All,
I'm using the below code to prevent some characters from Keypress and copying past in User Form, it is working smoothly but suddenly I realise that it is not fulfilling my one requirement.
My requirement is Fifth Digit of text must be Zero (0), I have prevented it from keypress but I want code to prevent it from copying past.
can someone help me with this?
I'm using the below code to prevent some characters from Keypress and copying past in User Form, it is working smoothly but suddenly I realise that it is not fulfilling my one requirement.
My requirement is Fifth Digit of text must be Zero (0), I have prevented it from keypress but I want code to prevent it from copying past.
can someone help me with this?
VBA Code:
Private Sub txtIFSC_Change()
Dim LastPosition As Long
Const PatternFilter As String = "*[!0-9A-Z]*"
Static LastText As String
Static SecondTime As Boolean
If Not SecondTime Then
With txtIFSC
If .Text Like PatternFilter Then
MsgBox "Special Character and Small Alphabet are not allowed in this tab", vbExclamation, "Invalid Character Found"
SecondTime = True
.Text = LastText
.SelStart = LastPosition
Else
LastText = .Text
End If
End With
End If
SecondTime = False
End Sub
Private Sub txtIFSC_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim LastPosition As Long
Const PatternFilter As String = "*[!0-9A-Z]*"
With txtIFSC
LastPosition = .SelStart
End With
End Sub
Private Sub txtIFSC_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Dim LastPosition As Long
Const PatternFilter As String = "*[!0-9A-Z]*"
With txtIFSC
LastPosition = .SelStart
Select Case Len(Me.txtIFSC.Text)
Case 4
If (KeyAscii > 47 And KeyAscii < 58) Or KeyAscii = 8 Then
KeyAscii = KeyAscii
Else
KeyAscii = 0
MsgBox "Fift Digit of {IFSC Code} Must be Zero", vbExclamation, "Incorrect IFSC Code"
End If
End Select
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Asc("A") To Asc("Z")
Case Else
KeyAscii = 0
MsgBox "Special Character and Small Alphabet are not allowed in this tab", vbExclamation, "Invalid Character Found"
End Select
End With
End Sub