0

I am using the following VBA Else-If code:

If ActiveSheet.Range(range_name).Value < "8.50%" Then
   ActiveSheet.Shapes.Range(Array(shape_name)).Select
    With Selection.ShapeRange.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(192, 0, 0)
        .Transparency = 0
        .Solid
    End With
    ElseIf ActiveSheet.Range(range_name).Value < "9.70%" Then
     ActiveSheet.Shapes.Range(Array(shape_name)).Select
    With Selection.ShapeRange.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(255, 255, 0)
        .Transparency = 0
        .Solid
    Else
     ActiveSheet.Shapes.Range(Array(shape_name)).Select
    With Selection.ShapeRange.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(0, 255, 0)
        .Transparency = 0
        .Solid
    End With

When I run this, I receive an error: "Else without If ". So, I changed the Else line to:

 ElseIf ActiveSheet.Range(range_name).Value >= "9.70%" Then

But still, it is giving me the same error, even though this Else has an If

Why is that? And the first code is also correct and shouldn't give an error in the first place. So why is this happening? Thanks in advance!

2 Answers 2

3

It's because you're missing an End With in the middle, i.e. this part

With Selection.ShapeRange.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(255, 255, 0)
        .Transparency = 0
        .Solid
Sign up to request clarification or add additional context in comments.

1 Comment

agree with sam. all With statement within your If statement should be ended.
1

add this:

End If

on the last line.
This is to terminate the if statement like what you did in your With statement.

2 Comments

Ya that that i wrote, i forgot to paste it here. Anyway thanks :)
anytime mate. :) we're glad to be of help. to avoid such, at the beginning of your code, end all statements that needs to be ended. then right your events in the middle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.