0

我正在使用以下 VBA Else-If代码:

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

当我运行它时,我收到一个错误:“Else without If”。因此,我将Else行更改为:

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

但是,它仍然给我同样的错误,即使这个Else有一个If

这是为什么?第一个代码也是正确的,一开始就不应该出错。那么为什么会这样呢?提前致谢!

4

2 回答 2

3

这是因为你End With在中间缺少一个,即这部分

With Selection.ShapeRange.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(255, 255, 0)
        .Transparency = 0
        .Solid
于 2013-10-30T05:43:26.803 回答
1

add this:

End If

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

于 2013-10-30T05:40:23.170 回答