user2006015
user2006015

Reputation: 11

using if function in textbox

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.

Upvotes: 0

Views: 2218

Answers (2)

whytheq
whytheq

Reputation: 35557

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

Upvotes: 0

bonCodigo
bonCodigo

Reputation: 14361

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"

Upvotes: 1

Related Questions