我创建了一个循环来检查列和列类型的长度。但是一旦它找到一个真实的情况,它就会结束select而不是进入下一个可能存在额外错误的情况(列)。
'(6.1)创建循环以检查格式和长度错误 xRowCount = 1 xFormatErrorCount = 0
For i = 0 To xNumOfRows - 1
xColA = Cells(xRowCount, 1).Value
xColB = Cells(xRowCount, 2).Value
xColC = Cells(xRowCount, 3).Value
xColD = Cells(xRowCount, 4).Value
xColE = Cells(xRowCount, 5).Value
xColF = Cells(xRowCount, 6).Value
xColG = Cells(xRowCount, 7).Value
xColH = Cells(xRowCount, 8).Value
xColI = Cells(xRowCount, 9).Value
xColJ = Cells(xRowCount, 10).Value
xColK = Cells(xRowCount, 11).Value
xColL = Cells(xRowCount, 12).Value
xColM = Cells(xRowCount, 13).Value
Select Case True
Case Len(xColA) > 2 'Check if length is greater than 2
Cells(xRowCount, 1).Interior.Color = RGB(255, 0, 0)
xFormatErrorCount = xFormatErrorCount + 1
Case Len(xColB) > 5 'Check if length is greater than 5
Cells(xRowCount, 2).Interior.Color = RGB(255, 0, 0)
xFormatErrorCount = xFormatErrorCount + 1
Case Len(xColC) > 5, Cells(xRowCount, 3).NumberFormat <> "0" 'Check if length is greater than 5 and format is number
Cells(xRowCount, 3).Interior.Color = RGB(255, 0, 0)
xFormatErrorCount = xFormatErrorCount + 1
Case Len(xColD) > 18, Cells(xRowCount, 4).NumberFormat <> "0" 'Check if length is greater than 18 and format is number
Cells(xRowCount, 4).Interior.Color = RGB(255, 0, 0)
xFormatErrorCount = xFormatErrorCount + 1
Case Len(xColE) > 11, Cells(xRowCount, 5).NumberFormat <> "0" 'Check if length is greater than 11 and format is number
Cells(xRowCount, 5).Interior.Color = RGB(255, 0, 0)
xFormatErrorCount = xFormatErrorCount + 1
Case Cells(xRowCount, 6).NumberFormat <> "0" 'Check if format is number
Cells(xRowCount, 6).Interior.Color = RGB(255, 0, 0)
xFormatErrorCount = xFormatErrorCount + 1
Case Cells(xRowCount, 7).NumberFormat <> "0" 'Check if #
Cells(xRowCount, 7).Interior.Color = RGB(255, 0, 0)
xFormatErrorCount = xFormatErrorCount + 1
Case Cells(xRowCount, 8).NumberFormat <> "0" 'Check if #
Cells(xRowCount, 8).Interior.Color = RGB(255, 0, 0)
xFormatErrorCount = xFormatErrorCount + 1
Case Cells(xRowCount, 9).NumberFormat <> "0" 'Check if #
Cells(xRowCount, 9).Interior.Color = RGB(255, 0, 0)
xFormatErrorCount = xFormatErrorCount + 1
Case Cells(xRowCount, 10).NumberFormat <> "0.00" 'Check if #
Cells(xRowCount, 10).Interior.Color = RGB(255, 0, 0)
xFormatErrorCount = xFormatErrorCount + 1
Case Len(xColK) > 1 'Check if length is greater than 1
Cells(xRowCount, 11).Interior.Color = RGB(255, 0, 0)
xFormatErrorCount = xFormatErrorCount + 1
Case Len(xColL) > 1 'Check if length is greater than 1
Cells(xRowCount, 12).Interior.Color = RGB(255, 0, 0)
xFormatErrorCount = xFormatErrorCount + 1
Case Len(xColM) > 1 'Check if length is greater than 1
Cells(xRowCount, 13).Interior.Color = RGB(255, 0, 0)
xFormatErrorCount = xFormatErrorCount + 1
End Select
xRowCount = xRowCount + 1
下一步
答案 0 :(得分:1)
这是VB select case的工作原理。
如果你想让多个案例执行同一个块,你可以将它们放在相同的大小写中并用逗号分隔它们,如下所示:
Public Sub TestX(ByVal x As Long)
Select Case x
Case 1, 2
Debug.Print "x is One or Two"
Case 3
Debug.Print "x is Three"
Case Else
Debug.Print ; "is something else"
End Select
End Sub
答案 1 :(得分:0)
在我看来,好像你不想要Select Case
,它的设计行为与你描述的方式完全相同,而是一系列单独的If语句:
Dim n As Long
If Len(xColA) > 2 Then 'Check if length is greater than 2
Cells(xRowCount, 1).Interior.Color = RGB(255, 0, 0)
xFormatErrorCount = xFormatErrorCount + 1
End If
If Len(xColB) > 5 Then 'Check if length is greater than 5
Cells(xRowCount, 2).Interior.Color = RGB(255, 0, 0)
xFormatErrorCount = xFormatErrorCount + 1
End If
If Len(xColC) > 5 Or Cells(xRowCount, 3).NumberFormat <> "0" Then 'Check if length is greater than 5 and format is number
Cells(xRowCount, 3).Interior.Color = RGB(255, 0, 0)
xFormatErrorCount = xFormatErrorCount + 1
End If
If Len(xColD) > 18 Or Cells(xRowCount, 4).NumberFormat <> "0" Then 'Check if length is greater than 18 and format is number
Cells(xRowCount, 4).Interior.Color = RGB(255, 0, 0)
xFormatErrorCount = xFormatErrorCount + 1
End If
If Len(xColE) > 11 Or Cells(xRowCount, 5).NumberFormat <> "0" Then 'Check if length is greater than 11 and format is number
Cells(xRowCount, 5).Interior.Color = RGB(255, 0, 0)
xFormatErrorCount = xFormatErrorCount + 1
End If
For n = 6 To 9
If Cells(xRowCount, n).NumberFormat <> "0" Then 'Check if format is number
Cells(xRowCount, n).Interior.Color = RGB(255, 0, 0)
xFormatErrorCount = xFormatErrorCount + 1
End If
Next n
If Cells(xRowCount, 10).NumberFormat <> "0.00" Then 'Check if #
Cells(xRowCount, 10).Interior.Color = RGB(255, 0, 0)
xFormatErrorCount = xFormatErrorCount + 1
End If
If Len(xColK) > 1 Then 'Check if length is greater than 1
Cells(xRowCount, 11).Interior.Color = RGB(255, 0, 0)
xFormatErrorCount = xFormatErrorCount + 1
End If
If Len(xColL) > 1 Then 'Check if length is greater than 1
Cells(xRowCount, 12).Interior.Color = RGB(255, 0, 0)
xFormatErrorCount = xFormatErrorCount + 1
End If
If Len(xColM) > 1 Then 'Check if length is greater than 1
Cells(xRowCount, 13).Interior.Color = RGB(255, 0, 0)
xFormatErrorCount = xFormatErrorCount + 1
End If
我认为你可以使用数组来简化这一点。