问题
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