2011-10-07 104 views
0

有沒有一種優雅的方式來寫一個如果A或B或C那麼X否則不做任何聲明?例如。緊湊的錯誤檢查

If WorksheetFunction.CountA(Range("J:J")) <> SymbolCount Then 
    MsgBox "Check column J in Estate worksheet for fill completion" 
End If 
If WorksheetFunction.CountA(Range("K:K")) <> SymbolCount Then 
    MsgBox "Check column K in Estate worksheet for fill completion" 
End If 

我能想到的最有創意的解決方案是增加的CountAs,看看它是否匹配符號計數* NumberOfTests,但是這將意味着放棄非特異性應答消息。

回答

1

這是一個嘗試(如果我深知你的問題):

Sub test() 
Dim SymbolCount As Integer, i As Integer 
Dim aCols(2) As String 
aCols(0) = "J" 
aCols(1) = "K" 

SymbolCount = 0 
For i = 0 To UBound(aCols) - 1 
    If WorksheetFunction.CountA(Range(aCols(i) & ":" & aCols(i))) <> SymbolCount Then 
     MsgBox "Check column " & aCols(i) & " in Estate worksheet for fill completion" 
    End If 
Next i 
End Sub