我正在尝试删除不等于所选组合框值的行。我有以下代码,但我得到运行时错误424对象需要。有人可以帮忙吗?
Dim wkss As Worksheet, wkJob As Worksheet
Dim i As Long
Set wkss = Sheets("TempWs6")
Set wsJob = Sheets("Job")
wkss.Select
For i = Cells(Rows.Count, 10).End(xlUp).Row To 1 Step -1
If Cells(i, 10) <> wsJob.ComboBox1.Value Then
wkss.Rows(i).Row.Delete
End If
Next i
非常感谢!
答案 0 :(得分:3)
要删除行,您需要使用EntireRow
:
wkss.Rows(i).EntireRow.Delete
应该这样做!
(对于它的价值,移除整个列也是如此,使用EntireColumn.Delete
)
修改:我们使用With
语句清除assigning parentage的范围和选择。
Dim wkss As Worksheet, wkJob As Worksheet
Dim i As Long
Set wkss = Sheets("TempWs6")
Set wsJob = Sheets("Job")
With wkss
For i = .Cells(.Rows.Count, 10).End(xlUp).Row To 1 Step -1
If .Cells(i, 10) <> wsJob.ComboBox1.Value Then
.Rows(i).EntireRow.Delete
End If
Next i
End With
.Cells([...]).Row to 1 Step -1
执行此操作:从最后一行开始(从...(xlUp).Row
获得),运行下面的代码,然后“向上”一行,然后再次运行,并重复直到你到达行1
。