VBA – 评估所有的陈述,当超过1可以是真实的

我想评估一个单元格的语句列表(例如,有顶部边框,有底部边框等),并将结果传递给一个集合对象。 但是,如果/ elseif在find第一个真正的语句时停止计算,对于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 

只需将elseif更改为if即可。 您将有N个if / endif块(其中N是您正在评估的属性的数量)。 那么当一个成功的时候,它就会转移到下一个。

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

等等

不能你只用

 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 . . . 

我build议你使用一系列If-Then语句,每一个你想检查的东西。

这对你有帮助吗?

 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