Excel VBA Macro: Delete Rows (Based on Cell Values in Multiple Columns)

preview_player
Показать описание
Excel VBA Macro: Delete Rows (Based on Cell Values in Multiple Columns)

Code:
Sub delete_rows_based_on_multi_col()

Dim row_count As Long
Dim i As Long
Dim ws As Worksheet

Set ws = ThisWorkbook.Sheets("Countries")

ws.Activate

i = 2

Do While i <= row_count

If Cells(i, 6) = "" _
Or Cells(i, 10) = "" _
Or Cells(i, 5) > 10000 Then

Rows(i).EntireRow.Delete
i = i - 1
row_count = row_count - 1
End If

i = i + 1

Loop

End Sub

#excelmacro #excelvba
Рекомендации по теме
Комментарии
Автор

You are Thank you!!

Quick Question. I want to delete duplicates. What code do I type to delete duplicates?

If Cells(i, 1).Value > 1 ?? <- this didn't work but I know its something like this.

Im trying to tell the code to delete rows that are both ...
*duplicates in column A or (i, 1)
AND if
*labeled "expired" in column C or (i, 3)

Do While i <= row_count
If Cells(i, 1) = "Duplicate" _
And Cells(i, 3) = "Expired" Then

Rows(i).EntireRow.Delete
i = i - 1
row_count = row_count - 1
End If

ItsTyara
Автор

Is there a reason why didn’t you do a For loop, this looks complicated

sakhilengwenya
Автор

I want to do something similar to this...
I have some records and I there's a column that may contain duplicate values and I want that based on a value that is on another column delete one specific row from my duplicates, meaning I just want to keep one specific row based on the values that column F (duplicate values) and column B (what determinates which row I have to delete). For example I have:
Row 1, Column B value = 0, Column F value = 123456
Row 2, Column B value = 1, Column F value = 123456
Based on that example I want to delete rows where my Column F value is duplicate and the column B value is 1. In that case Row 2 must be deleted and Row 1 needs to stay.

Is there any way of doing this using VBA?

josea.pascualr.
Автор

Why did we give i, 6 or i, 10 ? Cant we give just a universal condition for the entire row?

deepk
Автор

Can I loop it where the cells check against other cells on another worksheet and remove the rows from the first worksheet?

SMJ