2012-11-16 133 views
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, 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 

我不知道我在做什麼錯,任何幫助將非常多讚賞。

預先感謝您。

+0

您沒有提供所有的代碼。您的輸出樣本與您提供的代碼不匹配。 –

+0

@DanielCook,謝謝你的回覆,對不完整的信息道歉。代碼很長很複雜,因此我只粘貼了L變量。我希望這可以澄清問題。任何幫助和指導將非常感激。謝謝 – user1574185

回答

2

它看起來是最後的If/then將0.2加到你所有的5個輸入中,它應該:IsNumeric(Mid(Dn, 2, 1))對你所有的輸入都是正確的。

使用Select Case語句而不是所有的Ifs和​​ElseIfs,此代碼可能會有益於(可讀性,調試等)。

+0

非常感謝您的回覆,是的您的權利,最後如果聲明是騎過其他條件,所以我用類似的功能來解決我的問題。再次感謝您的時間和幫助。 – user1574185

+0

如果你想感謝他 - 你應該接受這個答案。 –

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