嵌套If循环编译错误VBA

时间:2018-04-05 14:09:46

标签: excel vba excel-vba

我收到了以下嵌套的If函数的编译错误。这一定很容易,我只是缺少你能发现编译错误吗?

If reduce_amount > 0 Then For Each rCell In HBWS.Range(Cells(3, Tickercolumn), Cells(LastHBR, Tickercolumn))
    If rCell.Value = TTB And rCell.Offset(0, -1).Value <> TickerTotalString And reduce_amount > 0 Then
    Cell_value = rCell.Offset(0, 3).Value
        If reduce_amount < Cell_value Then
        rCell.Offset(0, 3).Value = Cell_value - reduce_amount
        ElseIf reduce_amount > Cell_value Then
        rCell.Offset(0, 3).Value = 0
        reduce_amount = reduce_amount - Cell_value
        ElseIf reduce_amount = Cell_value Then
        reduce_amount = 0
        rCell.Offset(0, 3).Value = 0
        End If
    End If
Next
End If

还会理解导致编译错误的原因,以防止在进一步的迭代中发生这种情况。

由于

1 个答案:

答案 0 :(得分:2)

仅仅是为了回答和结束这个问题 - 关注Alex K评论

Sub test()
If reduce_amount > 0 Then
    For Each rCell In HBWS.Range(Cells(3, Tickercolumn), Cells(LastHBR, Tickercolumn))
    If rCell.Value = TTB And rCell.Offset(0, -1).Value <> TickerTotalString And reduce_amount > 0 Then
    Cell_value = rCell.Offset(0, 3).Value
        If reduce_amount < Cell_value Then
        rCell.Offset(0, 3).Value = Cell_value - reduce_amount
        ElseIf reduce_amount > Cell_value Then
        rCell.Offset(0, 3).Value = 0
        reduce_amount = reduce_amount - Cell_value
        ElseIf reduce_amount = Cell_value Then
        reduce_amount = 0
        rCell.Offset(0, 3).Value = 0
        End If
    End If
Next
End If
End Sub

将答案标记为社区维基