错误阅读选择案例 - excel vba

时间:2013-01-17 16:53:24

标签: excel-vba vba excel

我使用下面的代码,我无法使用以下语句:

If UCase(Left(Dn, 2)) = "LM" Then
    Dn.Offset(, 1) = Val(Mid(Dn, 2, 3)) + 3.5

代码不会执行上述语句中的任何内容。

Case "L"


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

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

        ElseIf UCase(Left(Dn, 6)) Like "LM###S" Then

        Dn.Offset(, 1) = Mid(Dn, 3, 3) + 2
        Else
        Dn.Offset(, 1) = Mid(Dn, 3, 3) + 0.2
        End If**



    ElseIf UCase(Left(Dn, 3)) = "L/M" Then
        Dn.Offset(, 1) = Val(Mid(Dn, 4, 2)) + 3.5
    ElseIf UCase(Left(Dn, 3)) = "LM$" Then
        Dn.Offset(, 1) = Val(Mid(Dn, 4, 2)) + 3.5
    ElseIf UCase(Left(Dn, 2)) = "L$" Then
        Dn.Offset(, 1) = Val(Mid(Dn, 3, 2)) + 2
    ElseIf UCase(Left(Dn, 3)) = "LO-" Then
        Dn.Offset(, 1) = Val(Mid(Dn, 4, 2)) + 2
    ElseIf UCase(Left(Dn, 6)) = "LO MID" Then
        Dn.Offset(, 1) = Val(Mid(Dn, 7, 3)) + 3.5
    ElseIf UCase(Left(Dn, 3)) = "LO " Then
        Dn.Offset(, 1) = Val(Mid(Dn, 4, 2)) + 2
    ElseIf UCase(Left(Dn, 5)) Like "L###H" Then
        Dn.Offset(, 1) = Val(Mid(Dn, 2, 3)) + 0.2
    ElseIf UCase(Left(Dn, 4)) Like "L##H" Then
        Dn.Offset(, 1) = Val(Mid(Dn, 2, 2)) + 0.2
    ElseIf UCase(Left(Dn, 4)) Like "L#" Then
        Dn.Offset(, 1) = Val(Mid(Dn, 2, 2)) + 0.2
    ElseIf IsNumeric(Mid(Dn, 3, 1)) And IsNumeric(Mid(Dn, 4, 1)) Then
        If Mid(Dn, 4, 1) = "0" And Mid(Dn, 3, 1) = "0" Then
            If Dn.Offset(, 3) = "CMBS" Then
                Dn.Offset(, 1) = Mid(Dn, 2, 3) + 20
            Else
                Dn.Offset(, 1) = Mid(Dn, 2, 3) + 2
            End If
        ElseIf UCase(Left(Dn, 5)) Like "L###S" Then
            Dn.Offset(, 1) = Mid(Dn, 2, 3) + 2
        Else
            Dn.Offset(, 1) = Mid(Dn, 2, 3) + 0.2
        End If
    ElseIf IsNumeric(Mid(Dn, 3, 1)) And IsNumeric(Mid(Dn, 4, 1)) Then
        If Mid(Dn, 4, 1) = "0" And Mid(Dn, 3, 1) <> "0" Then
            Dn.Offset(, 1) = Mid(Dn, 2, 3) + 0.2
        End If
    ElseIf UCase(Left(Dn, 3)) Like "L##" Then
        If IsNumeric(Mid(Dn, 2, 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
        End If
    End If

输入值 - &gt;
lm10,LM120S, LM120 期望的输出 - &gt;
13.5 123.5 120.35

非常感谢您对此问题的任何帮助。

3 个答案:

答案 0 :(得分:1)

Val(Mid(Dn, 2, 3))评估为零: M10 以字母开头,因此结束val进程。那应该是Val(Mid(Dn, 3, 2))

我假设您的select声明为Select Case UCase(Left(dn, 1))。如果你不是UCase文本,那么它将不会执行case语句中的任何代码

答案 1 :(得分:1)

将此代码粘贴到模块中

Public Function GetNumb(strString As String) As Double
    Dim strLen As Long, i As Long
    Dim Temp As String

    strLen = Len(strString): Temp = ""

    For i = 1 To strLen
        If (Mid(strString, i, 1) = "-") Then
            Temp = Temp & Mid(strString, i, 1)
        End If
        If (Mid(strString, i, 1) = ".") Then
            Temp = Temp & Mid(strString, i, 1)
        End If
        If (IsNumeric(Mid(strString, i, 1))) = True Then
            Temp = Temp & Mid(strString, i, 1)
        End If
    Next i

    If Len(Temp) = 0 Then
        GetNumb = 0
    Else
        GetNumb = CDbl(Trim(Temp))
    End If
End Function

然后更改您的代码

Dn.Offset(, 1) = Val(Mid(Dn, 2, 3)) + 3.5

Dn.Offset(, 1) = GetNumb(Dn.Value) + 3.5

解释:函数GetNumb从字符串中提取数字

  1. lm10 ~~&gt; 10
  2. LM120S ~~&gt; 120
  3. LM120 ~~&gt; 120

答案 2 :(得分:0)

在IF行周围放置一些Debug.Print语句,看看发生了什么,如下:

Debug,Print Dn
If UCase(Left(Dn, 2)) = "LM" Then
    Debug,Print "hi"
    Dn.Offset(, 1) = Val(Mid(Dn, 2, 3)) + 3.5

第一行的案例“L”是做什么的?你的代码是否阻止它到达IF语句?