Excel VBA – select案例结束select后,发现一个案件是真实的,并没有完成其他案件

我创build了一个循环来检查列和列types的长度。 但是,一旦它发现一个案件是真实的,它结束了select,而不是去下一个案件(列),可能会有一个额外的错误。

'(6.1)创build循环来检查格式和长度错误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 

下一个

它在我看来,就好像你不想要Select Case ,这是旨在performance你所描述的方式,而不是一系列单独的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 

我想你可以使用数组来简化这个。

这是VBselect案例的工作原理。

如果你想有多个执行同一个块的情况,你把它们放在同一个案例中,用逗号分隔它们,就像这样:

 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