PDA

View Full Version : Solved: nested looping



dansmith88
08-16-2012, 01:27 AM
Hi all,

I am new to vba and have created a loop to work through my data to delete all rows with an entry of texas in the state column. My problem arises though when i have an entry of texas consecutively. Does anyone have any ideas on how I can solve this problem? Maybe by using a nested loop? Any suggestions would be massively appreciated. Below you can see my code and an example of the table i am using.


Cheers



Sub Texas()
Dim sRow As Integer
sRow = 2
Cells(sRow, 5).Select
Do Until IsEmpty(ActiveCell.Value)
If ActiveCell.Value = "texas" Then
ActiveCell.Select
Selection.EntireRow.Delete
End If
sRow = sRow + 1
Cells(sRow, 5).Select
Loop

End Sub




the table is below

ErickAltenwerth77amariburghtexasotherno16Never tried itdemondSteuber35east isabellvilletexasSalmonyes18What is sushi?MARCIAdeckow47hoegerboroughNorth carolinatroutno18Hate it

Bob Phillips
08-16-2012, 01:35 AM
Work backwards

Sub Texas()
Dim lastrow As Long
Dim i As Long

Application.ScreenUpdating = False

With ActiveSheet

lastrow = .Cells(.Rows.Count, "E").End(xlUp).Row
For i = lastrow To 2 Step -1

If .Cells(i, "E").Value = "texas" Then

.Rows(i).Delete
End If
Next i
End With

Application.ScreenUpdating = True
End Sub

dansmith88
08-16-2012, 01:59 AM
Thank you thats fantastic