0

I have the following code behind a userform

Private Sub add_button()
    On Error Resume Next
     If TextBox1 > TextBox9 Then
         TextBox12 = "YES"
     Else
         TextBox12 = "No"
         If TextBox8 > TextBox3 And TextBox8 < TextBox4 Then
            TextBox11 = "YES"
         Else
            TextBox11 = "no"
            If TextBox12 = "NO" Then
                TextBox10 = "NO"
            ElseIf TextBox11 = "NO" Then
                TextBox10 = "NO"
            Else
                TextBox10 = "YES"
            End If
         End If
     End If
End Sub

The above code does not work: please advice on possible errors.

2
  • 1
    Hi, I think the problem is CASE SENSITIVE. if you want insentive comparsion, put option compare text on top of your module, or use ucase() to wrap your string. Commented Jan 24, 2013 at 3:33
  • 2
    On Error Resume Next Can you please remove this? and then tell us what doesn't work and what error are you gettng? Commented Jan 24, 2013 at 5:35

2 Answers 2

1

Few things,

  1. Remove error handling you will then know if there's an issue in execution as Siddharth's comment
  2. Balance both left right comparison for text formats/ cases as Larry's comment
  3. Use proper properties to get set values. In you case it is Textbox.Text
  4. Run through the code in debug mode by pressing F8 and adding break points
  5. Do Debug.Print or a Msgbox at each if-else to ensure the logic flow
  6. Respond to comments as the community is trying their best to help you solve your issue.

Here is change you could do to your code. At this point of the logic you have already set both 12 and 11 to NO in that case it is unnecessary to do this check:

 If TextBox12 = "NO" Then 
     TextBox10 = "NO" 
 ElseIf TextBox11 = "NO" Then 
     TextBox10 = "NO"

You may simply set Textbox10.Text = "NO"

Sign up to request clarification or add additional context in comments.

Comments

0

Please can you check that the actual conditions of the script are working as I'm a little unsure how well these conditions will work against text. Just copy out all the existing code and copy the following in as a test:

Private Sub add_button()
    If TextBox1 > TextBox9 Then
        MsgBox "TextBox1 > TextBox9 "
    Else
        MsgBox "TextBox1 < TextBox9 "
        If TextBox8 > TextBox3 And TextBox8 < TextBox4 Then
            MsgBox "TextBox8 > TextBox3 And TextBox8 < TextBox4 "
        End
    End If
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.