Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

if and loops

ResolvedVersion 2010

Silvia has attended:
No courses

If and Loops

Can someone see what is wrong with my code.
It keeps saying that "Compile error: Else without If", but i am pretty sure you have to use elseif. Thanks in advance!

Sub Orders()
Range("c5").Select

Do Until ActiveCell = ""
If ActiveCell > 100 Then ActiveCell.Offset(0, 1) = "Empty"

ElseIf ActiveCell > 400 Then ActiveCell.Offset(0, 1) = "Re-order"

Else
ActiveCell.Offset(0, 1).Value = "Full"

End If

ActiveCell.Offset(1, 0).Select
Loop

End Sub

RE: If and Loops

Hello Silvia,

Thank you for your question. The code is fine, apart from these two lines:

If ActiveCell > 100 Then ActiveCell.Offset(0, 1) = "Empty"

ElseIf ActiveCell > 400 Then ActiveCell.Offset(0, 1) = "Re-order"

They should look like this:

If ActiveCell > 100 Then
ActiveCell.Offset(0, 1) = "Empty"

ElseIf ActiveCell > 400 Then
ActiveCell.Offset(0, 1) = "Re-order"

When you have more than one if statement, e.g. If as well as Elseif or Else, you need to "break" the first line of each statement after "Then".

I'm sure your code will work when you make this change.

Kind regards
Marius Barnard
STL

Mon 18 Dec 2017: Automatically marked as resolved.

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Excel tip:

Sorting List Subtotals

If you find that you would prefer to show the items in a subtotalled list in a different order, eg ascending rather than descending, you can sort your list. To sort a subtotalled list, hide the detail rows and then sort the subtotal rows. When you sort a subtotalled list, the hidden detail rows are automatically moved with the subtotal rows.
IMPORTANT: If you do not hide the details rows before sorting a subtotalled list, your subtotals will be removed and all of the rows in your list will be reordered.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.1 secs.