Excel VBA: Delete Rows in a Column based on a Range of Cells in another Column
I have the above Excel sheet and have been trying to use VBA to delete rows based on the values in column B, provided they match values currently residing in column C. I'm a coding novice and have so far only gotten to a point of managing to delete a row by selecting a single cell in column C (There was a row containing the value 331013 in column B and I managed to delete it by selecting the cell in column C with that that value (currently the first row) and it was deleted.
What I want to do is to select the entire range of values in column C so I can delete all of the matching values in column B in a single batch.
The following is what I've managed so far based on a lot of googling, it is working with just one selection, but when I manually select more than 1 row it just deletes the corresponding value of the first selection and if I select the entire column range I get a mismatch error. And I've also previously gotten a stack overflow error
Sub DeleteRows() Dim rng As Range Dim InputRng As Range Dim DeleteRng As Range Dim DeleteStr As Range xTitleId = "Input" Set InputRng = Application.Selection Set DeleteStr = Application.Selection Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8) Set DeleteStr = Application.InputBox("delete range :", xTitleId, DeleteStr.Address, Type:=8) For Each rng In InputRng If rng.Value = DeleteStr Then If DeleteRng Is Nothing Then Set DeleteRng = rng Else Set DeleteRng = Application.Union(DeleteRng, rng) End If End If Next DeleteRng.EntireRow.Delete End Sub
I'd really appreciate whatever input can be given.