【发布时间】:2014-04-16 00:42:58
【问题描述】:
我编写了一些简单的代码,将一个工作表中的单元格与另一个工作表中的单元格匹配,然后如果单元格相等则删除整行。
代码正确选择行,但由于某种原因拒绝实际删除我的工作表中的行。编辑:一些行删除。其他人则没有,即使它们具有与删除的值完全相同的值。如果有人可以提供帮助,将不胜感激。
Sub delFunds()
Dim fCell As Range 'Fund cell
Dim fRng As Range 'Fund range
Dim wCell As Range 'Working sheet cell
Dim wRng As Range 'Working sheet range
Dim n As Long
Set fRng = Worksheets("Funds").Range("C2:C117")
Set wRng = Worksheets("Working sheet").Range("I3:I7483")
For Each fCell In fRng.Cells 'Loop through all funds
For Each wCell In wRng.Cells 'Loop through all working cells
If StrComp(wCell.Value, fCell.Value, vbTextCompare) = 0 Then 'If equal then delete
n = wCell.Row
Rows(n & ":" & n).Select
Selection.Delete Shift:=xlUp
End If
Next wCell
Next fCell 'Go to next fund
End Sub
【问题讨论】:
-
您需要对行本身进行操作,还是只使用
wCell范围?wCell.EntireRow.Delete会在整个脚本的上下文中工作吗? -
我需要删除实际的行。执行 wCell.EntireRow.Delete 并不会实际删除单元格所在的行。