循环中出现溢出错误

时间:2018-04-20 00:24:54

标签: excel vba loops overflow

我需要一些帮助,我的代码收到溢出错误,我无法找出原因。

基本上,如果数据与前一行匹配,我想删除一行。

调试在c = c - 1

处停止

我让它在每个循环中更新最后一行,以便在表的末尾停止。

clastrow = ActiveWorkbook.Worksheets("ClaimData").Cells( _
                            claim.Rows.Count, "A").End(xlUp).Row

For c = 3 To clastrow

    If claim.Range("A" & c).Value = claim.Range("A" & c - 1).Value And _
           claim.Range("E" & c).Value = claim.Range("E" & c - 1).Value Then

        claim.Range("K" & c - 1).Value = claim.Range("K" & c - 1).Value + _
                                         claim.Range("K" & c).Value
        claim.Rows(c).Delete
        a = c
        c = a - 1
        clastrow = ActiveWorkbook.Worksheets("ClaimData").Cells( _
                                  claim.Rows.Count, "A").End(xlUp).Row

    End If
Next c

1 个答案:

答案 0 :(得分:0)

Work from the bottom up:

With claim

    clastrow = .Cells(.Rows.Count, "A").End(xlUp).Row

    For c = clastrow To 3 Step -1

        If .Cells(c, "A").Value = .Cells(c - 1, "A").Value And _
           .Cells(c, "E").Value = .Cells(c - 1, "E").Value Then

            .Cells(c - 1, "K").Value = .Cells(c - 1, "K").Value + .Cells(c, "K").Value
            .Rows(c).Delete

        End If
    Next c

End With