紧凑的错误检查

时间:2011-10-07 14:25:09

标签: vba

是否有一种优雅的方式来编写一个如果A或B或C然后X其他什么都不做声明? E.g。

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并查看它是否与SymbolCount * NumberOfTests匹配,但这意味着提供非特定的响应消息。

1 个答案:

答案 0 :(得分: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