2017-04-23 46 views
-1

有人可以请指教。对于下面的代码,我不断收到“未找到”错误消息。我已经确定关闭了for循环中包含的所有if语句。我无法弄清楚问题所在。下一个没有错误信息w /(关闭,如果语句)vba

代码本身应该循环遍历单个列并标识空格。当它完成并且满足限制条件时,它应该用新的计算值替换空白单元格。

LR = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row 
For i = LR To 2 
If Cells(i, 4) = "" Then 'if cell of interest is blank' 
    If Cells(i, 4).Offset(1, 0) <> "" Then 'if cell below is not blank' 
     If Cells(i, 4).Offset(1, 0) <> 0 Then 'if cell below is not zero' 
      If Cells(i, 4).Offset(1, -3) = Cells(i, 4).Offset(0, -3) Then 'iso_o = iso_o' 
       If Cells(i, 4).Offset(1, -2) = Cells(i, 4).Offset(0, -2) Then 'iso_d = iso_d' 
        If Cells(i, 4).Offset(1, 2) <> "" Then 'if gdp_o not blank' 
         If Cells(i, 4).Offset(1, 3) <> "" Then 'if gdp_d not blank' 
          avg = ((Cells(i, 4).Offset(1, 2).Value + Cells(i, 4).Offset(1, 3).Value)/Cells(i, 4).Offset(-1, 0).Value) 'create ratio of gdp to tradeflow' 
          Cells(i, 4).Value = ((Cells(i, 4).Offset(0, 2) + Cells(i, 4).Offset(0, 3))/avg) 'create new flow based on ratio of gdp tradeflow' 
         ElseIf Cells(i, 4).Offset(1, 3) = "" Then 'if gdp_d blank' 
          Cells(i, 4).Value = 0 
         End If 
        ElseIf Cells(i, 4).Offset(1, 2) = "" Then 'if gdp_o is blank' 
         Cells(i, 4).Value = 0 
       ElseIf Cells(i, 4).Offset(1, -2) <> Cells(i, 4).Offset(0, -2) Then 'iso_d ne iso_d' 
        Cells(i, 4).Value = 0 
       End If 
      ElseIf Cells(i, 4).Offset(1, -3) <> Cells(i, 4).Offset(0, -3) Then 'iso_o ne iso_o' 
       Cells(i, 4).Value = 0 
      End If 
     ElseIf Cells(i, 4).Offset(1, 0) = 0 Then 'if cell below is zero' 
      If Cells(i, 4).Offset(-1, 0) <> "" Then 'if cell above is not blank' 
       If Cells(i, 4).Offset(-1, 0) <> 0 Then 'if cell above is not 0' 
        If Cells(i, 4).Offset(-1, -3) = Cells(i, 4).Offset(0, -3) Then 'if iso_o = iso_o' 
         If Cells(i, 4).Offset(-1, -2) = Cells(i, 4).Offset(0, -2) Then 'if iso_d = iso_d' 
          If Cells(i, 4).Offset(-1, 2) <> "" Then 'if gdp_o not blank' 
           If Cells(i, 4).Offset(-1, 3) <> "" Then 'if gdp_d not blank' 
            avg = ((Cells(i, 4).Offset(-1, 2) + Cells(i, 4).Offset(-1, 3))/Cells(i, 4).Offset(-1, 0)) 'create ratio, tradeflow above and gdp's above' 
            Cells(i, 4).Value = ((Cells(i, 4).Offset(0, 2) + Cells(i, 4).Offset(0, 3))/avg) 
           ElseIf Cells(i, 4).Offset(-1, 3) = "" Then 'if gdp_d blank' 
            Cells(i, 4).Value = 0 
           End If 
          ElseIf Cells(i, 4).Offset(-1, 2) = "" Then 'if gdp_o blank' 
           Cells(i, 4).Value = 0 
          End If 
         ElseIf Cells(i, 4).Offset(-1, -2) <> Cells(i, 4).Offset(0, -2) Then 'if iso_d ne iso_d' 
          Cells(i, 4).Value = 0 
         End If 
        ElseIf Cells(i, 4).Offset(-1, -3) <> Cells(i, 4).Offset(0, -3) Then 'if iso_o = iso_o' 
         Cells(i, 4).Value = 0 
        End If 
       ElseIf Cells(i, 4).Offset(-1, 0) = 0 Then 'if cell 1 up is zero' 
        Cells(i, 4).Value = 0 'flow equals 0' 
       End If 
      End If 
     End If 
    ElseIf Cells(i, 4).Offset(1, 0) = "" Then 
     If Cells(i, 4).Offset(-1, 0) <> "" Then 'if cell above is not blank' 
      If Cells(i, 4).Offset(-1, 0) <> 0 Then 'if cell above is not 0' 
       If Cells(i, 4).Offset(-1, -3) = Cells(i, 4).Offset(0, -3) Then 'if iso_o = iso_o' 
        If Cells(i, 4).Offset(-1, -2) = Cells(i, 4).Offset(0, -2) Then 'if iso_d = iso_d' 
         If Cells(i, 4).Offset(-1, 2) <> "" Then 'if gdp_o not blank' 
          If Cells(i, 4).Offset(-1, 3) <> "" Then 'if gdp_d not blank' 
           avg = ((Cells(i, 4).Offset(-1, 2) + Cells(i, 4).Offset(-1, 3))/Cells(i, 4).Offset(-1, 0)) 'create ratio, tradeflow above and gdp's above' 
           Cells(i, 4).Value = ((Cells(i, 4).Offset(0, 2) + Cells(i, 4).Offset(0, 3))/avg) 
          ElseIf Cells(i, 4).Offset(-1, 3) = "" Then 'if gdp_d blank' 
           Cells(i, 4).Value = 0 
          End If 
         ElseIf Cells(i, 4).Offset(-1, 2) = "" Then 'if gdp_o blank' 
          Cells(i, 4).Value = 0 
         End If 
        ElseIf Cells(i, 4).Offset(-1, -2) <> Cells(i, 4).Offset(0, -2) Then 'if iso_d ne iso_d' 
         Cells(i, 4).Value = 0 
        End If 
       ElseIf Cells(i, 4).Offset(-1, -3) <> Cells(i, 4).Offset(0, -3) Then 'if iso_o = iso_o' 
        Cells(i, 4).Value = 0 
       End If 
      ElseIf Cells(i, 4).Offset(-1, 0) = 0 Then 'if cell 1 up is zero' 
       Cells(i, 4).Value = 0 'flow equals 0' 
      End If 
     ElseIf Cells(i, 4).Offset(-1, 0) = "" Then 'If cell 1 up is blank' 
      Cells(i, 4).Value = "" 
     End If 
    End If 
ElseIf Cells(i, 4) <> "" Then 'if cell of interest is blank' 
End If 
Next i 

谢谢! 本

+0

缩进是你的朋友,在这种情况下,你已经正确地缩进你'If'语句(虽然你真的应该缩进'If'语句,以便它关闭是从'For' ...'Next'级别的一个缩进),所以很容易发现从一个缩进六个缩进级别的行到仅缩进四个缩进级别的行 - 这将是错误。 – YowE3K

回答

2

你的第六If不与End If