VBA Excel代码的输出不正确

时间:2012-11-16 15:21:27

标签: excel-vba vba excel

我有以下代码:

    Case "L"
     If 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)) = "LM" Then
 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) + 3.5
            Else
            Dn.Offset(, 1) = Mid(Dn, 3, 3) + 0.35
            End If
            End If


    ElseIf UCase(Left(Dn, 2)) = "L$" Then
            Dn.Offset(, 1) = Val(Mid(Dn, 3, 2)) + 2

     ElseIf UCase(Left(Dn, 3)) = "LOW" Then
            Dn.Offset(, 1) = Val(Mid(Dn, 4, 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###S" Then
      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) + 2
        Else
            Dn.Offset(, 1) = Mid(Dn, 2, 3) + 0.2
        End If
        End If
     '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
    'ElseIf UCase(Left(Dn, 1)) = "L" Then
            'Dn.Offset(, 1) = Val(Mid(Dn, 2, 3)) + 2
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
            'ElseIf IsNumeric(Mid(Dn, 2, 1)) And IsNumeric(Mid(Dn, 3, 1)) Then
            'Dn.Offset(, 1) = IIf(Mid(Dn, 3, 1) = 0, Val(Mid(Dn, 2, 2)) + 2, Val(Mid(Dn, 2, 2)) + 0.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 UCase(Left(Dn, 5)) Like "L###S" Then
      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) + 2
        Else
            Dn.Offset(, 1) = Mid(Dn, 2, 3) + 0.2
        End If
        End If


    ElseIf UCase(Left(Dn, 4)) Like "L##S" 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, 2, 2) + 2
        Else
            Dn.Offset(, 1) = Mid(Dn, 2, 2) + 0.2
        End If
        End If
        End If


      If IsNumeric(Mid(Dn, 2, 1)) Then
             Dn.Offset(, 1) = Val(Mid(Dn, 2, 2)) + 0.2
     End If

       'ElseIf UCase(Left(Dn, 4)) Like "L###" Then
           ' Dn.Offset(, 1) = Val(Mid(Dn, 2, 3)) + 0.2
    ' End If

我目前的代码输出如下:

*L111 --> 111.2
*L100 --> 100.2
*L100S --> 100.2
*L10S --> 12.2
*L12S --> 12.2

所需的输出如下:

*L111 --> 111.2
*L100 --> 100.2
*L100S --> 100
*L10S --> 12
*L12S --> 12.2

我不确定我做错了什么,非常感谢任何帮助。

提前谢谢。

2 个答案:

答案 0 :(得分:2)

看起来最终的If / Then正在为所有五个输入添加0.2,它应该:IsNumeric(Mid(Dn, 2, 1))对所有输入都是真的。

这段代码可能会因使用Select Case语句而不是所有的Ifs和​​ElseIf而受益(在可读性,调试等方面)。

答案 1 :(得分:0)

感谢上面的建议和帮助,但我设法解决了我自己的问题。

Case "L"
     If 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)) = "LM" Then
 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) + 3.5
            Else
            Dn.Offset(, 1) = Mid(Dn, 3, 3) + 0.35
            End If
            End If


    ElseIf UCase(Left(Dn, 2)) = "L$" Then
            Dn.Offset(, 1) = Val(Mid(Dn, 3, 2)) + 2

     ElseIf UCase(Left(Dn, 3)) = "LOW" Then
            Dn.Offset(, 1) = Val(Mid(Dn, 4, 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###S" Then
      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) + 2
        Else
            Dn.Offset(, 1) = Mid(Dn, 2, 3) + 0.2

        End If
        End If

   ElseIf UCase(Left(Dn, 4)) Like "L##S" 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, 2, 2) + 2
       Else
          Dn.Offset(, 1) = Mid(Dn, 2, 2) + 0.2
       End If
        End If


   ElseIf UCase(Left(Dn, 4)) Like "L###" Then
      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) + 0.2
        Else
            Dn.Offset(, 1) = Mid(Dn, 2, 3) + 0.2

        End If
        End If

   ElseIf UCase(Left(Dn, 3)) Like "L##" 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, 2, 2) + 0.2
       Else
          Dn.Offset(, 1) = Mid(Dn, 2, 2) + 0.2
       End If
       End If

    End If