VBA - 当超过1时,可以评估所有语句

时间:2011-08-15 11:17:05

标签: excel vba excel-vba comparison

我想评估Cell的语句列表(例如,有顶部边框,有底部边框等),并将结果传递给集合对象。但是,如果/ elseif在找到第一个真实语句时将停止评估,则Select Case将保持同样。

有没有其他方法可以用来做到这一点?

Dim BorderColl As Collection
Set BorderColl = New Collection 

If RngCell.Borders(xlDiagonalDown).LineStyle <> xlNone Then

  BorderColl.Add "xlDiagonalDown", LCase("xlDiagonalDown")

ElseIf RngCell.Borders(xlDiagonalUp).LineStyle <> xlNone Then

  BorderColl.Add "xlDiagonalUp", LCase("xlDiagonalUp")

ElseIf RngCell.Borders(xlEdgeBottom).LineStyle <> xlNone Then

  BorderColl.Add "xlEdgeBottom", LCase("xlEdgeBottom")

ElseIf RngCell.Borders(xlEdgeLeft).LineStyle <> xlNone Then

  BorderColl.Add "xlEdgeLeft", LCase("xlEdgeLeft")

ElseIf RngCell.Borders(xlEdgeRight).LineStyle <> xlNone Then

  BorderColl.Add "xlEdgeRight", LCase("xlEdgeRight")

ElseIf RngCell.Borders(xlEdgeTop).LineStyle <> xlNone Then

  BorderColl.Add "xlEdgeTop", LCase("xlEdgeTop")

End If

4 个答案:

答案 0 :(得分:6)

不能使用

Dim BorderColl As Collection
Set BorderColl = New Collection 

If RngCell.Borders(xlDiagonalDown).LineStyle <> xlNone Then
    BorderColl.Add "xlDiagonalDown", LCase("xlDiagonalDown")
End If
If RngCell.Borders(xlDiagonalUp).LineStyle <> xlNone Then
    BorderColl.Add "xlDiagonalUp", LCase("xlDiagonalUp")
End If
.
.
.

答案 1 :(得分:3)

我建议您使用一系列If-Then语句,每个语句都要检查一次。

答案 2 :(得分:3)

只需将elseif更改为if即可。您将拥有N if / endif块(其中N是您正在评估的属性数)。然后当一个人成功时,它将继续前进到下一个。

if someproperty then: do something
if someOtherProperty then: do something else

等等

答案 3 :(得分:1)

这对你有帮助吗?

Dim rngcell As Range, lBorder As Long, sStyles As Variant

    sStyles = Split(",,,,XLDIAGONALDOWN,XLDIAGONALUP,XLEDGELEFT,XLEDGETOP,XLEDGEBOTTOM,XLEDGERIGHT,XLINSIDEVERTICAL,XLINSIDEHORIZONTAL", ",")

    Set rngcell = Range("A1")
    For lBorder = 5 To 12
        If rngcell.Borders(lBorder).LineStyle <> xlNone Then
            Debug.Print lBorder, sStyles(lBorder - 1)
        End If
    Next