I have the following two columns (simplified)
Bond BMW Equity Amazon Bond Netflix Bond Uber Equity Google
I want a code that basically goes through the first column and deletes the row if it is an
My idea (in terms of the for-loop):
for i = 1 to 5 if ws.Cells(i,1).Value = "Bond" Then Else ws.Rows(i).Select End If Next i Selection.Delete End Sub
The problem that I have noticed is that only the last cell to be selected is deleted. I would have thought that all the cells I selected in the loop will be selected. Is there a specific way to select multiple. (
Union would not work in this case, as it the list will be dynamic).
Normally when deleting rows, you would start at the bottom and work your way up.
Sub Button2_Click() Dim LstRw As Long, x LstRw = Cells(Rows.Count, "A").End(xlUp).Row For x = LstRw To 1 Step -1 If Cells(x, "A").Value = "Equity" Then Cells(x, "A").EntireRow.Delete End If Next x End Sub
To keep the activity with the sheet to a minimum try it like that
Sub Button2_Click() Dim DeleteRange As Range Dim LstRw As Long, x As Long LstRw = Cells(Rows.Count, "A").End(xlUp).Row For x = 1 To LstRw If Cells(x, "A").Value = "Equity" Then If DeleteRange Is Nothing Then Set DeleteRange = Rows(x) Else Set DeleteRange = Union(DeleteRange, Rows(x)) End If End If Next x If Not (DeleteRange Is Nothing) Then DeleteRange.EntireRow.Delete End If End Sub
Also no need to loop backwards as the deletion is done at the end.