【问题标题】:Compare cells to delete rows, value is true but not deleting rows比较单元格删除行,值为真但不删除行
【发布时间】:2016-11-23 11:16:17
【问题描述】:

我正在尝试比较 2 个单元格,如果该行将被删除,我尝试使用 msgbox 返回值并显示其为真,但行没有被删除。 第一个单元格是使用一张表格中的公式得出的,另一个只是数字,这有什么不同吗?

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

【问题讨论】:

  • 删除行时,您希望从下到上移动或在实际删除行时跳过递增行计数器。此外,如果您不使用 MsgBox 的返回值 - 不要使用括号。你的比较逻辑似乎没问题。在最顶端Dim r, s, i, t As Long 表示 r、s、i 是 Variant,只有 t 是 Long。所以似乎是那些小事在某个地方搞砸了你的逻辑。
  • 谢谢!我重新定义了变体,它起作用了。

标签: vba excel


【解决方案1】:

删除时总是从最后一个索引到第一个。这适用于列表框、组合框、范围等。

如果你从头到尾删除,那么你将跳过每隔一行


【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多