Should be simple but I'm stuck on this:
I want to clean up large files by deleting all rows where certain cells do (or dont) contain certain values.
I'm fairly new to vba, so I did what I usually do and cobbled up my script from posts here and on other forums, and then resolved the runtime errors that inevitably pop up, but after that I got stuck...
Here's my code so far:
If I run it, it deletes the first row (which it should, the cell in the first column doesn't contain an "A"), but that's it. In the following rows, the value of the first cell alternates between "A" and "B", so it should delete half of these as well, but it simply doesn't, it just stops there, no errors.
I suppose the problem has something to do with the union part, but I can't find a solution, as I don't even really know what to google for. I found some posts where people used "Range()" around "ActiveCell.EntireRow" but that just throws another runtime error for me.
Now here's an detail that may be interesting: If I comment out the "mySel.Delete" and then run the code, the first line (which got deleted before I commented it out) is not even selected.
Any ideas?
I want to clean up large files by deleting all rows where certain cells do (or dont) contain certain values.
I'm fairly new to vba, so I did what I usually do and cobbled up my script from posts here and on other forums, and then resolved the runtime errors that inevitably pop up, but after that I got stuck...
Here's my code so far:
Code:
Sub DN_test()
Dim myRange, mySel As Range
Dim delVal As String
delVal = "A"
Set myRange = ActiveSheet.UsedRange.Columns(1).Cells
For Each cell In myRange
If cell.Value <> delVal Then
If mySel Is Nothing Then
Set mySel = ActiveCell.EntireRow
Else
Set mySel = Union(mySel, ActiveCell.EntireRow)
End If
End If
Next cell
mySel.Delete
End Sub
If I run it, it deletes the first row (which it should, the cell in the first column doesn't contain an "A"), but that's it. In the following rows, the value of the first cell alternates between "A" and "B", so it should delete half of these as well, but it simply doesn't, it just stops there, no errors.
I suppose the problem has something to do with the union part, but I can't find a solution, as I don't even really know what to google for. I found some posts where people used "Range()" around "ActiveCell.EntireRow" but that just throws another runtime error for me.
Now here's an detail that may be interesting: If I comment out the "mySel.Delete" and then run the code, the first line (which got deleted before I commented it out) is not even selected.
Any ideas?