2015-05-25 101 views
0

我真的需要這個項目的幫助,我正在處理,但我似乎無法從我的背上得到這個錯誤。誰能請推薦解決方案?編譯錯誤:END IF如果沒有塊IF(持續錯誤)

Sub Dsurvey() 
    Dim i As Integer 
    Dim j As Integer 
    Dim k As Integer 

    k = 3 

    For i = 3 To 22 
     j = j + 1 
     If Sheets("2").Cells(j, "A").Value - Sheets("2").Cells(i, "A").Value = Sheets("2").Cells(j, "B").Value - Sheets("2").Cells(i, "B").Value Then 
      Sheets("2").Cells(k, "J").Value = Sheets("2").Cells(i, "A").Value 
      Sheets("2").Cells(k, "K").Value = Sheets("2").Cells(i, "B").Value 
      Do While Sheets("2").Cells(k, "J").Value <= Sheets("2").Cells(j, "A").Value 
        Sheets("2").Cells(k, "J").Value = Sheets("2").Cells(k - 1, "J").Value + 100 
        Sheets("2").Cells(k, "K").Value = Sheets("2").Cells(k - 1, "K").Value + 100 
        k = k + 1 

     End If 
    Next i 

End Sub 

回答

2

也不是那麼多,你缺少的End If,但你不能用Loop關閉Do While

k = 3 
For i = 3 To 22 
    j = j + 1 
    If Sheets("2").Cells(j, "A").Value - Sheets("2").Cells(i, "A").Value = Sheets("2").Cells(j, "B").Value - Sheets("2").Cells(i, "B").Value Then 
     Sheets("2").Cells(k, "J").Value = Sheets("2").Cells(i, "A").Value 
     Sheets("2").Cells(k, "K").Value = Sheets("2").Cells(i, "B").Value 
     Do While Sheets("2").Cells(k, "J").Value <= Sheets("2").Cells(j, "A").Value 
      Sheets("2").Cells(k, "J").Value = Sheets("2").Cells(k - 1, "J").Value + 100 
      Sheets("2").Cells(k, "K").Value = Sheets("2").Cells(k - 1, "K").Value + 100 
      k = k + 1 
     Loop 
    End If 
Next i 

Debug版本:

k = 3 
With Sheets("2") 
    For i = 3 To 22 
     j = j + 1 
     If IsNumeric(.Cells(j, "A")) And IsNumeric(.Cells(i, "A")) And IsNumeric(.Cells(j, "B")) And IsNumeric(.Cells(i, "B")) Then 
      If .Cells(j, "A").Value - .Cells(i, "A").Value = .Cells(j, "B").Value - .Cells(i, "B").Value Then 
       .Cells(k, "J").Value = .Cells(i, "A").Value 
       .Cells(k, "K").Value = .Cells(i, "B").Value 
       Do While .Cells(k, "J").Value <= .Cells(j, "A").Value 
        .Cells(k, "J").Value = .Cells(k - 1, "J").Value + 100 
        .Cells(k, "K").Value = .Cells(k - 1, "K").Value + 100 
        k = k + 1 
       Loop 
      End If 
     Else 
      Debug.Print "Not numeric: & " & i & Chr(45) & j & Chr(45) & k 
     End If 
    Next i 
End With 
+0

非常感謝你的幫助。它現在在我的if語句中顯示類型不匹配。任何想法的這個錯誤的來源? –

+0

看起來至少有一個不是數值;因此'類型不匹配'在數學運算中不能使用非數值。 – Jeeped

+0

謝謝,我會交叉檢查正在操作的單元格。 –