【发布时间】: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。所以似乎是那些小事在某个地方搞砸了你的逻辑。 -
谢谢!我重新定义了变体,它起作用了。