Mistake in Code??

charllie

Well-known Member
Joined
Apr 6, 2005
Messages
986
Hi Folks,

Could someone tell me where i am going wrong please with the following code.

Code:
Private Sub TextBox4_Change()
    If Workbooks("Beck.xls").Worksheets("Time Sheet Compile").Range("BL6") = "HANDOVER" Then
        If Len(Me.TextBox4.Value) > 5 Then   'only fire off if the code is 6 characters long or more
            Dim ctl As MSForms.Control
            Dim i As Integer
                With Workbooks("Team Leader.xls").Sheets("QC Check Sheet Archive")
                    Private Sub TextBox4_Change()
    If Workbooks("Beck.xls").Worksheets("Time Sheet Compile").Range("BL6") = "HANDOVER" Then
        If Len(Me.TextBox4.Value) > 5 Then   'only fire off if the code is 6 characters long or more
            Dim ctl As MSForms.Control
            Dim i As Integer
                With Workbooks("Team Leader.xls").Sheets("QC Check Sheet Archive")
                    For i = 5 To 200
                        If Me.TextBox4.Text = .Cells(i, "C") Then
                            Exit Sub
                        Else
                            MsgBox "HELP"
                            Exit Sub
                        End If
                    Next
                End With
        End If
    End If
End Sub

With this part of the code:
Code:
If Me.TextBox4.Text = .Cells(i, "C") Then
                            Exit Sub
                        Else
                            MsgBox "HELP"
                            Exit Sub
                        End If



I am trying to say TextBox4.text is the same as what is in rows C then do nothing.
But if it is does not match what is in row c then show msgbox"HELP".


The error i am getting is no matter what i put in the textbox4 the msgbox"HELP" always shows.

Thanks
 
Hi Erik,

I tend to prefer MATCH rather than FIND, mainly because I know Match but not FIND - didnt I read somewhere that FIND will return true for a partial match?

Alan
 
Upvote 0
Hi Erik/Alan,

So what is the best way to go round this then so it will look at the whole column. Please.

Thanks
 
Upvote 0
Perhaps use a different loop construction, a Do While, for example:

Code:
Dim found As Boolean
Dim i As Long

    found = False
    i = 5
    Do
        If Me.TextBox4.Text = Cells(i, "C") Then found = True
        i = i + 1
    Loop While i <= 200 And Not found

HTH
 
Upvote 0
Hi Taz,

Thanks for the advice. However i still can't get it to work. Here is the modified code i am using, where am i going wrong please?

Code:
If Workbooks("Beck.xls").Worksheets("Time Sheet Compile").Range("BL6") = "HANDOVER" Then
        If Len(Me.TextBox4.Value) > 5 Then   'only fire off if the code is 6 characters long or more
            Dim found As Boolean
              Dim i As Long
                With Workbooks("Team Leader.xls").Sheets("QC Check Sheet Archive")
                    found = False
                    i = 5
                    Do
                        If Me.TextBox4.Text = Cells(i, "C") Then found = True
                            i = i + 1
                            Loop While i <= 200 And Not found
                        Else
                            MsgBox "You Have Entered The Wrong Order Number, Please Try Again"
                            Me.TextBox4 = ClearContents
                            Exit Sub
                        End If
                    Next
                End With
        End If
    End If

Thanks
 
Upvote 0
Hi Folks,

I am really struggling with this one, not sure what i am doing and cant get the code to work.

Can anyone out there help me please.

Thanks
 
Upvote 0
Hi mike,

Not looked at previous posts on his, but isnt this (untested code) what you want:
Code:
If Workbooks("Beck.xls").Worksheets("Time Sheet Compile").Range("BL6").Text = "HANDOVER" Then
    If Len(Me.TextBox4.Value) > 5 Then   'only fire off if the code is 6 characters long or more
        Dim Found As Boolean
        Dim i As Long
        With Workbooks("Team Leader.xls").Sheets("QC Check Sheet Archive")
            Found = False
            For i = 5 To 200
                If Me.TextBox4.Text = Cells(i, "C").Text Then
                    Found = True
                    Exit For
                Else
                    MsgBox "You Have Entered The Wrong Order Number, Please Try Again"
                    Me.TextBox4 = ClearContents
                    Exit Sub
                End If
            Next i
        End With
    End If
End If
 
Upvote 0
Oh, this is a confusing thread :-?
FIND, MATCH, DO WHILE, FOR EACH, FOR NEXT
:warning: don't loop when you can do it usinginbuilt functions for the purpose:warning:

charllie,
you want to find Me.TextBox4.Text in column C
that's the only thing which is really clear to me
I'm sure this shouldn't be a complicated issue

my suggestions is to post some sample data and what you expect the macro to do
sorry if I'm wrong and missing something, but this seems to me the best solution

kind regards,
Erik
 
Upvote 0

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