Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dodgy if statement need help 1

Status
Not open for further replies.

maverickmonster

Programmer
May 25, 2004
194
0
0
GB
I have an If statement in a VBA excel application but it does not seam to work properly, it does not seam to fire the right statements they must be done in this order could any one help or make any suggestions ?

Thanks

If (Sheets(1).Cells(X, 55) = "Y") Then
GoTo pro1
ElseIf (Sheets(1).Cells(X, 54) = "C" And Sheets(1).Cells(X, 56) = "N") Then
GoTo pro2
ElseIf (Sheets(1).Cells(X, 45) = "V") Or (Sheets(1).Cells(X, 45) = "C") And Sheets(1).Cells(X, 54) = "U" And Sheets(1).Cells(X, 55) = "N" And Sheets(1).Cells(X, 56) = "Y" And (Sheets(1).Cells(X, 45) = "I") Or (Sheets(1).Cells(X, 45) = "F") Then
GoTo pro3
ElseIf (Sheets(1).Cells(X, 45) = "V") Or (Sheets(1).Cells(X, 45) = "C") And (Sheets(1).Cells(X, 54) = "U") Or (Sheets(1).Cells(X, 54) = "C") And Sheets(1).Cells(X, 55) = "N" And Sheets(1).Cells(X, 56) = "Y" And (Sheets(1).Cells(X, 56) = "N") Then
GoTo pro4
ElseIf (Sheets(1).Cells(X, 45) = "I") Or (Sheets(1).Cells(X, 45) = "F") Then
GoTo pro5
ElseIf ((Sheets(1).Cells(X, 45) = "V") Or (Sheets(1).Cells(X, 45) = "C")) And Sheets(1).Cells(X, 54) = "U" And Sheets(1).Cells(X, 55) = "N" And Sheets(1).Cells(X, 56) = "N" And Sheets(1).Cells(X, 45) = "F" Then
GoTo pro6
ElseIf (Sheets(1).Cells(X, 45) = "V") Or (Sheets(1).Cells(X, 45) = "C") And Sheets(1).Cells(X, 54) = "U" And Sheets(1).Cells(X, 55) = "N" And Sheets(1).Cells(X, 56) = "N" And Sheets(1).Cells(X, 45) = "I" Then
GoTo pro7
ElseIf (Sheets(1).Cells(X, 45) = "V") Or (Sheets(1).Cells(X, 45) = "C") And Sheets(1).Cells(X, 54) = "U" And Sheets(1).Cells(X, 55) = "N" And Sheets(1).Cells(X, 56) = "N" And Sheets(1).Cells(X, 45) = "N" Then
GoTo pro8
 
Well for starters (as far as i can see)

Code:
ElseIf (Sheets(1).Cells(X, 45) = "V") Or (Sheets(1).Cells(X, 45) = "C") And Sheets(1).Cells(X, 54) = "U" And Sheets(1).Cells(X, 55) = "N" And Sheets(1).Cells(X, 56) = "Y" And (Sheets(1).Cells(X, 45) = "I") Or (Sheets(1).Cells(X, 45) = "F") Then
        GoTo pro3

will never fire because you are checking if the value of Sheets(1).Cells(X, 45) is either V or C and either I or F. As the value of Sheets(1).Cells(X, 45) can't be both either V or C and either I or F then this will never fire.

I suggest you use more brackets around the various parts of your If/ElseFf statements so that it is clearer (to yourself and others)what exactly they are testing. This would be a good first step if nothing else.

HTH

Dave
 
Ooops pushed enter too soon

The brackets will also sort out any order of precedence issues that may be causing your code not to work, I believe that 'And' has a higher order of precedence than 'Or' but not 100% certain on that.

Dave

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top