Compare cells to delete rows, value is true but not deleting rows

流过昼夜 提交于 2020-01-10 06:11:45

问题


I'm trying to compare 2 cells and if its true that row will be deleted, i tried using msgbox to return the value and it shows its true, but row is not deleting. The first cell is derived using formula in 1 sheet and the other is just numbers, does that make a difference?

Dim r, s, i, t As Long
Dim com, cc, bl, acc As Long
Dim rDB, rInput As Range
Dim shDB, shInput As Worksheet

Set shDB = ActiveWorkbook.Sheets("Database")
Set rDB = shDB.Range("A1", "T1000")

Set shInput = ActiveWorkbook.Sheets("Data Input")
Set rInput = shInput.Range("A1", "R1000")

r = 2
Do While Len(shDB.Cells(r, 1).Formula) > 0
    com = shInput.Cells(7, 5).Value
    cc = shInput.Cells(5, 5).Value
    bl = shInput.Cells(9, 5).Value
    acc = shInput.Cells(5, 10).Value
    MsgBox (com & " " & shDB.Cells(r, 1).Value & " " & cc & " " & rDB.Cells(r, 2).Value & " " & rDB.Cells(r, 3).Value & " " & bl & " " & rDB.Cells(r, 4).Value & " " & acc)
    If shDB.Cells(r, 1).Value = com And rDB.Cells(r, 2).Value = cc And rDB.Cells(r, 3).Value = bl And rDB.Cells(r, 4).Value = acc Then
    shDB.Rows(r).EntireRow.Delete
    MsgBox ("deleting rows")
    Else
        r = r + 1
    End If
Loop

回答1:


When deleting alway go from the last index to the first. This applies to listboxes, comboboxes, ranges, ...etc.

If you delete from first to last then you will skip every other row




来源:https://stackoverflow.com/questions/40755334/compare-cells-to-delete-rows-value-is-true-but-not-deleting-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!