下一个没有错误消息w /(关闭if语句)vba

时间:2017-04-23 18:45:41

标签: excel vba excel-vba for-loop if-statement

请有人劝告。我不断收到以下代码的“next without for”错误消息。我已经确保关闭for循环中包含的所有if语句。我无法弄清楚问题是什么。

代码本身应该遍历单个列并标识空格。如果这样做,并且满足限制,则应该用新的计算值替换空白单元格。

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 

谢谢! 本

1 个答案:

答案 0 :(得分:2)

您的第六个If未与End If

关闭
相关问题