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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

nested If-Then-Else 2

Status
Not open for further replies.

OscarAlberto

Programmer
Dec 4, 2002
15
CR
I cannot make a large but simple nested If-Then-ElseIf-End If statement. I need to go through several cells from down-up, checking first, if the cell's contents is > 0, and if a number from a previous operation is > 0.

The procedure goes directly from the first If to the last End If without going through all the other If's.
 
Without seeing your code it sounds like the first IF is evaluating to True, so no reason to go further.
Post your code and let someone look at it
Michael
 
Thank you. It seems the problem was that I was using more than one And after each If.

Is that how it works? No way to test a number, cell content, etc. more than twice with And?
 
You can use multiple ANDs with If as long as you use parentheses to force Boolean conditions for evaluation. For example:

If (A > B) AND (B > C) AND (C > D) Then
' Do something here.
End If

Or something like this.

If (A > B) AND ((B > C) AND (C > D)) Then
' Do something here.
End If
 
Why would you want a big nested IF. Wrap it into a loop:-

Sub RunThruCells()

Dim LastRw As Long
Dim Rng As Range

Application.ScreenUpdating = False
LastRw = Cells(Rows.Count, "A").End(xlUp).Row
Set Rng = Range(Cells(1, "A"), Cells(LastRw, "A"))

For x = Rng.Rows.Count To 1 Step -1
If Cells(x, 1) = 21 And Cells(x, 2).Value > 5 Then
Cells(x, 1).Offset(0, 2).Value = "Here"
End If
Next x
Application.ScreenUpdating = True

End Sub

This runs thru every used cell in Col A (From Bottom Up) and looks to see if the value in that cell is equal to 21 AND the value in Col B is greater than 5. If it finds a match it will then put "Here" in Col C.

Regards
Ken...................
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top