Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

fixing type mismatch

0

hello  

after  many  tries  and    search  so  much   in  the  internet   to  find  solution    my  problem   I don't   find  any  thing  I applied   many  suggestion  from  many  websities with  many posts   are  similer   my  post   nothing   successes

 so  what  I  want    set  focus  in  textbox1  if   the  values  in textbox 7   is  matched with  column c 

I  really  apprecite  if  any  body  help  to  solve  this  dilemma

Answer
Discuss

Answers

0
Selected Answer

Leopard

This works for me- see attached file. Notice it's on the KeyDown event (so your code has been deleted in my file) and it intercepts the Enter or Tab which would normally shift focus within the Frame. If there's no match in column C, it changes the key code (so the form doesn't "see" those keys and the focus remains in that box, now cleared).

Important changes are in bold below:

Private Sub TextBox7_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

If Not (KeyCode = 13 Or KeyCode = 9) Then Exit Sub 'do nothing if key pressed isn't 13(Enter) or 9(Tab)

Dim SRC As Range
Dim ss As String
    ss = TextBox7.Value
    Set SRC = Sheets("DATA").Range("c:c").Find(ss, LookIn:=xlValues, lookat:=xlWhole)
    If SRC Is Nothing Then
        TextBox7 = "" 'clear unmatched input
        KeyCode = 0 'change the keycode to keep focus
    Else
        MsgBox "The value " & ss & " is matched" 'confirm and allow focus to move
    End If
End Sub
Discuss

Discussion

Just noticed the code from Variatus and his comment about testing. For my file too, you need to click the button in DATA then enter values in TextBox7 of the displayed form and pressEnter or Tab.
John_Ru (rep: 6152) Jan 31, '21 at 9:31 am
@Variatus    thanks   for  make  clear  how  the  event     should   work   you're  right
leopard (rep: 88) Jan 31, '21 at 1:08 pm
@John    good  choice    about  event  keydown    it's  better 
many  thanks 
leopard (rep: 88) Jan 31, '21 at 1:10 pm
Add to Discussion
0

Hello Leopard,

This code should do what you want.

Private Sub TextBox7_Exit(ByVal Cancel As MSForms.ReturnBoolean)

    Dim Src     As Range
    Dim Ss      As String

    With Frame1
        Ss = .TextBox7.Value
        Set Src = Sheets("DATA").Range("c:c").Find(Ss, LookIn:=xlValues, lookat:=xlWhole)
        If Src Is Nothing Then
            .TextBox7.Value = ""
            Cancel = True
        Else
            MsgBox "the value is matched"
            .TextBox1.SetFocus
        End If
    End With
End Sub
Discuss

Discussion

thanks variatus  but  after  i  close  the form     it  shows  again  the  message  box   and  show  the  error  may   you  fix it  ,please?
leopard (rep: 88) Jan 31, '21 at 9:11 am
This isn't from the code we are talking about. It's from the way you test it. Change the test to get a different result. But you don't want to run the code in a test. You want it to run in your project. Therefore you should include the code by which you open the form and by which you close the form in your test.
Bear in mind that the code I provide runs when you exit TextBox7. So, after you close the MsgBox, if you click on TextBox7 again the procedure will run again if you close the form after that. That is correct behaviour.The Exit procedure ran again in your test because you clicked on TextBox7 again. This will only happen during a test but not when your project runs in real life. In real life there will be no MsgBox and if the user clicked on TextBox7 again he will see TextBox1 selected again - no problem.
Variatus (rep: 4889) Jan 31, '21 at 9:23 am
Note that if TextBox1 is empty, Ss will be "" and the search will find Src at the first cell in column C. That will produce the result like a match was found. To avoid that you should add a test, like, If Len(Ss) Then, don't run the search and select TextBox7 again as if the match wasn't found.
Variatus (rep: 4889) Jan 31, '21 at 9:30 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login