Delete rows not equal to combobox value?

不羁的心 提交于 2020-01-16 02:03:06

问题


I am trying to delete rows that are not equal to a combobox value that is selected. I have the following code, but i get a run-time error 424 Object required. Can anyone assist?

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

Thanks much!


回答1:


To delete a row, you'd use EntireRow:

wkss.Rows(i).EntireRow.Delete should do it!

(For what it's worth, the same goes for removing an entire column, use EntireColumn.Delete)

Edit: Let's clear up the ranges and selections by assigning parentage with a With statement.

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

The .Cells([...]).Row to 1 Step -1 does this: Starting at your last row (which you get from ...(xlUp).Row), run the code below, then "step" up one row, and run again, and repeat until you reach row 1.



来源:https://stackoverflow.com/questions/34954675/delete-rows-not-equal-to-combobox-value

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