View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Andrew[_3_] Andrew[_3_] is offline
external usenet poster
 
Posts: 15
Default Why does this loop only NEARLY work?!

Hi all

I have the following code. The theory is that the code loops through a
range supplied by name, and looks for matches in a set of data.

The code almost works! But not quite.

Many rows are removed, but some remain. However, running the procedure
a second time results in all the remaining matches being removed.

Anyone any ideas? Incidentally, I've been testing it on an export of
the Orders table from Northwind, with a second sheet holding 3 or 4
countries for which I want to remove data.

Thanks very much,

Andrew

Sub RemoveSpecifiedRows()
Dim strCriteriaRange As String, strCol As String
Dim rngCriteria As Range
Dim rngCell1 As Range
Dim rngCell2 As Range
Dim intCounter As Integer

strCriteriaRange = InputBox("Enter name of range containing the
criteria values")
strCol = InputBox("Enter the column letter to search")
Set rngCriteria = Range(strCriteriaRange)
Application.ScreenUpdating = False

For Each rngCell1 In rngCriteria.Cells
For Each rngCell2 In ActiveSheet.Columns(strCol).Cells
If rngCell2.Value = rngCell1.Value Then
rngCell2.EntireRow.Delete shift:=xlShiftUp
intCounter = intCounter + 1
End If
Next rngCell2
Next rngCell1

Application.ScreenUpdating = True
MsgBox intCounter & " rows were successfully deleted"

End Sub