选择案例错误2007

时间:2016-08-09 11:39:27

标签: vba excel-vba excel

我在数组中循环并检查单元格值,但是,有时可能会发生已检查的单元格为“ #DIV / 0!”,然后我的宏断开。怎么改呢?我尝试了两件事,但没有任何积极的影响。

vRange = Range(Cells(ShiftRow, 4), Cells(ShiftRow - 3, TheLastColumn)).Value

Dim i As Integer

For i = 4 To TheLastColumn
    Select Case vRange(4, i)
        Case Is = "#DIV/0!"
            vRange(1, i) = ""
        Case Is = "1"
            vRange(1, i) = vRange(3, i)
        Case Is = "2"
            vRange(1, i) = vRange(3, i - 1)
        Case Is = "3"
            vRange(1, i) = vRange(3, i - 2)
        Case Is = "I"
            vRange(1, i) = vRange(3, i)
        Case Is = "II"
            vRange(1, i) = vRange(3, i - 1)
        Case Is = "III"
            vRange(1, i) = vRange(3, i - 2)
        Case Else
            vRange(1, i) = ""

    End Select
Next i

我也尝试过:Case CVErr(xlErrDiv0)

4 个答案:

答案 0 :(得分:2)

CStr([#DIV/0!])返回字符串"Error 2007",因此您可以像这样更改代码

vRange = Range(Cells(ShiftRow - 3, 4), Cells(ShiftRow, TheLastColumn)).Value ' the second cell is usualy the bottom right, but Excel takes care of that
Dim i As Integer

For i = 4 To UBound(vRange, 2)
    Select Case CStr(vRange(4, i))
        Case "1", "I":     vRange(1, i) = vRange(3, i)
        Case "2", "II":    vRange(1, i) = vRange(3, i - 1)
        Case "3", "III":   vRange(1, i) = vRange(3, i - 2)
        Case "Error 2007": vRange(1, i) = "" ' optional because Case Else can handle it
        Case Else:         vRange(1, i) = ""
    End Select
Next i

答案 1 :(得分:1)

尝试使用On Error Resume Next捕获错误,并添加以下代码而不是您的选择案例:

Dim i As Integer

On Error Resume Next
For i = 4 To TheLastColumn

    Select Case vRange(4, i)
        Case Is = "#DIV/0!"
            If err.Number = 13 Then ' this is Error number 13 when deviding by zero
                vRange(1, i) = ""
                err.Clear
            End If

        Case Is = "1"
            vRange(1, i) = vRange(3, i)
        Case Is = "2"
            vRange(1, i) = vRange(3, i - 1)
        Case Is = "3"
            vRange(1, i) = vRange(3, i - 2)
        Case Is = "I"
            vRange(1, i) = vRange(3, i)
        Case Is = "II"
            vRange(1, i) = vRange(3, i - 1)
        Case Is = "III"
            vRange(1, i) = vRange(3, i - 2)
        Case Else
            vRange(1, i) = ""

    End Select

Next i

答案 2 :(得分:0)

包含错误的单元格比非错误单元格返回different data type(子类型错误的变体)。这使比较变得复杂。

一种方法是将一个case语句嵌套在另一个case语句中。外壳检查单元格类型(错误/非错误)。内部案例根据非错误值选择要做的事情。我已使用TypeName function来确定我正在使用的数据类型。

此示例适用于活动单元格。更新要测试的值。

Sub test()
' Update contents of active cell to test.
' Test cases:
'
'   Value   Output
'   1/0     Error: Error 2007
'   1       One
'   Hi      Hi
'   <other> Other


    ' Outer case checks for errors.
    Select Case TypeName(ActiveCell.Value)
        Case "Error"
            ' We have an error.
            MsgBox "Error: " & CStr(CVErr(ActiveCell.Value))

        Case Else
            ' Non-error.
            ' Inner case checks cell content.
            Select Case ActiveCell.Value
                Case 1
                    ' User entered 1.
                    MsgBox "One"

                Case "Hi"
                    ' User entered "Hi".
                    MsgBox "Hi"

                Case Else
                    ' Unknown user entry.
                    MsgBox "Other"

            End Select
    End Select
End Sub

您可以通过添加第二个内部案例来改进此子句,该案例会检查不同的错误代码。

答案 3 :(得分:0)

进一步了解Slai的出色答案(如果您认为此答案有帮助,请提高Slai的答案,该问题的可接受答案),此信息可能对将来的读者有所帮助。

我有一个.csv文件(纯文本),在该文件中,我正在运行一个脚本以着色不是数字的任何单元格。因此,对于每个单元格,我都在运行此功能:

Function fnIsNumber(Value) As Boolean
   fnIsNumber = Evaluate("ISNUMBER(0+""" & Value & """)")
End Function

某些单元格具有(纯文本-请记住,这是一个csv)值#DIV/0!。上面的函数在这些单元格上终止,并显示消息Run-time error '13': Type mismatch

在Excel宏中,甚至字符串 #DIV/0!的计算结果为“错误2007”,因此我不得不按照上述Slai的建议修改函数(请赞成他的帮助!)

Function fnIsNumber(Value) As Boolean
   If CStr(Value) = "Error 2007" Then
      fnIsNumber = False
   Else
      fnIsNumber = Evaluate("ISNUMBER(0+""" & Value & """)")
   End If
End Function

问题解决了。谢谢Slai。