VBA - Evaluate all Statements when more than 1 can be true
I want to evaluate a list of statements for a Cell (e.g. has top border, has bottom border etc.) and pass the findings to a collection object. However If/elseif will stop evaluating when it finds the first true statement, same holds for Select Case.
Is there any other way that I can use to do this?
Dim BorderColl As Collection
Set BorderColl = New Collection
If RngCell.Borders(xlDiagonalDown).LineStyle <> xlNone Then
BorderColl.Add "xlDiagonalDown", LCase("xlDiagonalDo开发者_如何学Gown")
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
Cant you just use
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
.
.
.
?
Simply change the elseif to if. You will have N if/endif blocks (where N is the number of properties you are evaluating). Then when one is successful it will just move on to the next one.
if someproperty then: do something
if someOtherProperty then: do something else
etc etc
I would suggest you use a series of If-Then statements, one for each thing you want to check.
Does this help you?
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
精彩评论