运行时错误 - 类型不匹配

时间:2012-11-06 10:53:53

标签: excel-vba vba excel

我有一个宏,它将特定的vales添加到特定的字符串。但是我目前遇到运行时错误,我无法弄清楚为什么?

Case "L"

    If UCase(Left(Dn, 3)) = "L/M" Then
        Dn.Offset(, 1) = Val(Mid(Dn, 4, 2)) + 35

    ElseIf UCase(Left(Dn, 2)) = "LM" Then
        'Dn.Offset(, 1) = Val(Mid(Dn, 3, 3)) + 3.5
        If IsNumeric(Mid(Dn, 3, 1)) And IsNumeric(Mid(Dn, 4, 1)) Then
            If Mid(Dn, 4, 1) = "0" Then
                Dn.Offset(, 1) = Mid(Dn, 3, 2) + 3.5
            Else
                Dn.Offset(, 1) = Mid(Dn, 3, 2) + 0.35
            End If
        End If

        If IsNumeric(Mid(Dn, 4, 1)) And IsNumeric(Mid(Dn, 5, 1)) Then
            If Mid(Dn, 5, 1) = "0" Then
                Dn.Offset(, 1) = Mid(Dn, 3, 3) + 35
            Else
                Dn.Offset(, 1) = Mid(Dn, 3, 3) + 0.35
            End If
        End If

    ElseIf UCase(Left(Dn, 3)) = "LOW" Then
        Dn.Offset(, 1) = Val(Mid(Dn, 4, 2)) + 20
    ElseIf UCase(Left(Dn, 3)) = "LO-" Then
        Dn.Offset(, 1) = Val(Mid(Dn, 4, 2)) + 20
    ElseIf UCase(Left(Dn, 6)) = "LO MID" Then
        Dn.Offset(, 1) = Val(Mid(Dn, 7, 3)) + 35
    ElseIf UCase(Left(Dn, 2)) = "L+" Then
        Dn.Offset(, 1) = Num
    ElseIf UCase(Left(Dn, 3)) = "LO " Then
        Dn.Offset(, 1) = Val(Mid(Dn, 4, 2)) + 20


        'ElseIf UCase(Left(Dn, 1)) = "L" Then
        'Dn.Offset(, 1) = Val(Mid(Dn, 2, 3)) + 2
        'ElseIf IsNumeric(Mid(Dn, 2, 1)) Then
        'Dn.Offset(, 1) = IIf(IsNumeric(Mid(Dn, 2, 1) + Mid(Dn, 3, 1)), Val(Mid(Dn, 2, 3)) + 2, Val(Mid(Dn, 2, 1)) + 0.2)

    ElseIf IsNumeric(Mid(Dn, 2, 1)) And IsNumeric(Mid(Dn, 3, 1)) Then
        If Mid(Dn, 3, 1) = "0" Then
            Dn.Offset(, 1) = Mid(Dn, 2, 2) + 2
        Else
            Dn.Offset(, 1) = Mid(Dn, 2, 2) + 0.2
        End If
    Else
        Dn.Offset(, 1) = Val(Mid(Dn, 2, 3)) + 20
    End If
    If IsNumeric(Mid(Dn, 3, 1)) And IsNumeric(Mid(Dn, 4, 1)) Then
        If Mid(Dn, 4, 1) = "0" Then
            Dn.Offset(, 1) = Mid(Dn, 2, 3) + 20
        Else
            Dn.Offset(, 1) = Mid(Dn, 2, 3) + 0.2
        End If

    End If

输入数据

*vh105 --> 105.9
*h107 --> 107.8
*l107 --> 107.2
*lm106 --> 106.35
*lm106  
*l107   
*44 

非常感谢任何有关此问题的帮助。

1 个答案:

答案 0 :(得分:0)

隔离Val(Mid(Dn,4,2))甚至是Mid(Dn,4,2)因为我认为Val没有从Mid函数中获取正确的数字字符串

在第2行和第3行之间使用:

  

Debug.Print Mid(Dn,4,2)
Debug.Print Val(Mid(Dn,4,2))

告诉我们您在立即窗口中获得的内容(如果不存在,请转到查看>立即窗口)