if and loops

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

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » If and Loops

If and Loops

resolvedResolved · High Priority · Version 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:

How to apply the same formatting and data to multiple sheets at the same time in Excel 2010

When you want to format more than one sheet in a worksheet exactly the same way, Ctrl-click the tabs of the sheets you want to group together and they will all turn white. While they are grouped, anything you enter in one sheet gets entered into the others.

After you have done this, remember to click on the tabs to take them out of the group so that you do not accidentally insert data in multiple sheets when you just want to insert data in one.

View all Excel hints and tips


Server loaded in 0.08 secs.