编译错误'案例没有选择案例'添加检查语句时

时间:2017-07-11 18:55:04

标签: excel vba excel-vba

这是为了重命名文件。大多数文件都有一定的字符长度,所以我可以轻松修改它们。虽然一个长度是相似的。我正在尝试添加另一个检查以查看该单元格是否是MID(LEN函数之后的所有数字。

添加IsNumeric(-(Mid(aCell, 2, Len(aCell) - 5)))检查时出现编译时错误:

  

没有选择案例的案例

我哪里错了?

以下工作正常:

Case 13
    Check = Left(aCell, 1)
        If Check = "e" Then 'Existing Standard
        val = "S-" & Left(aCell, Len(aCell) - 13) & Mid(aCell, 2, Len(aCell) - 10) & "-" & Mid(aCell, 5, Len(aCell) - 9)
            Else 'Standard after page 9
            val = "S-" & Left(aCell, Len(aCell) - 10) & "-" & Mid(aCell, 4, Len(aCell) - 9)
        End If
    Check = ""

我正在尝试将此代码添加到上面的Case 13代码中。

    Check = IsNumeric(-(Mid(aCell, 2, Len(aCell) - 5)))
        If Check = True Then 'Existing Three Line Diagrams
        val = "S-" & Mid(aCell, 3, Len(aCell) - 10) & "-" & Mid(aCell, 6, Len(aCell) - 9)

完整代码:

Option Explicit

Sub Convert()
Application.ScreenUpdating = False

Dim rng As Range, aCell As Range
Dim val As String, Check
Dim LastRow As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Set rng = Range("A2:A" & LastRow)

For Each aCell In rng.Cells
Select Case Len(aCell)
    Case 12
        Check = Left(aCell, 1)
            If Check = "0" Or Check = "c" Or Check = "e" Then 'Three Line Diagram
            val = "S-" & Mid(aCell, 2, Len(aCell) - 9) & "-" & Mid(aCell, 5, Len(aCell) - 8)
                Else 'Standard
                val = "S-" & Left(aCell, Len(aCell) - 9) & "-" & Mid(aCell, 4, Len(aCell) - 8)
            End If
        Check = ""
    Case 13 '|||Problem Area|||
        'Check = IsNumeric(-(Mid(aCell, 2, Len(aCell) - 5)))
            'If Check = True Then 'Existing Three Line Diagrams
            'val = "S-" & Mid(aCell, 3, Len(aCell) - 10) & "-" & Mid(aCell, 6, Len(aCell) - 9)
                'Else
                Check = Left(aCell, 1)
                If Check = "e" Then 'Existing Standard
                val = "S-" & Left(aCell, Len(aCell) - 13) & Mid(aCell, 2, Len(aCell) - 10) & "-" & Mid(aCell, 5, Len(aCell) - 9)
                    Else 'Standard after page 9
                    val = "S-" & Left(aCell, Len(aCell) - 10) & "-" & Mid(aCell, 4, Len(aCell) - 9)
            End If
        Check = ""
    Case 14 'Existing Standard after page 9
        val = "S-" & Left(aCell, Len(aCell) - 14) & Mid(aCell, 2, Len(aCell) - 11) & "-" & Mid(aCell, 5, Len(aCell) - 10)
    Case 15 'SD Standard
        val = "SD-" & Left(aCell, Len(aCell) - 15) & Mid(aCell, 5, Len(aCell) - 12) & "-" & Mid(aCell, 8, Len(aCell) - 12)
    Case 16 'Reference or Removal
        val = Left(aCell, Len(aCell) - 9) & "-" & (Mid(aCell, 8, Len(aCell) - 12))
    Case 17 'Reference or Removal after page 9
        val = Left(aCell, Len(aCell) - 10) & "-" & (Mid(aCell, 8, Len(aCell) - 13))
 On Error GoTo whoa
    Case Else 'All other pages
        val = "_Mod " & Left(aCell, Len(aCell) - 4)
End Select

val = UCase(val)

val = val & " " & aCell.Offset(, 2) & aCell.Offset(, 3)

aCell.Offset(, 1).Value = val
Next
Call RemoveZero
Call RemoveBadChar
    Range("C1").Select
    Worksheets("Rename").Columns("B").AutoFit
    Application.ScreenUpdating = True
whoa:
Application.ScreenUpdating = True
Exit Sub
End Sub

1 个答案:

答案 0 :(得分:1)

使用该代码,您需要额外的End If

Case 13
        Check = IsNumeric(-(Mid(aCell, 2, Len(aCell) - 5)))
        If Check = True Then 'Existing Three Line Diagrams
            val = "S-" & Mid(aCell, 3, Len(aCell) - 10) & "-" & Mid(aCell, 6, Len(aCell) - 9)
        Else
            Check = Left(aCell, 1)
        End If '''' THIS WAS MISSING

        If Check = "e" Then  'Existing Standard
            val = "S-" & Left(aCell, Len(aCell) - 13) & Mid(aCell, 2, Len(aCell) - 10) & "-" & Mid(aCell, 5, Len(aCell) - 9)
        Else                 'Standard after page 9
            val = "S-" & Left(aCell, Len(aCell) - 10) & "-" & Mid(aCell, 4, Len(aCell) - 9)
        End If

        Check = ""
Case 14
....

目前,您有If / Else / If / Else,如果没有结束,那么您可以在第二个之前使用。{ (或者当然,将该块更改为If / ElseIf / ElseIf / ... / End If或类似的东西)。