Better way to do a loop, small inquiry

thamestowers

New Member
Joined
Jun 8, 2014
Messages
23
Good day everyone!

I have this code, that for some reason it is not being performed entirely over the used range

E.g. There are 30 rows, ONLY from project "B" if I say in the box that I want the Project "A" it does not delete all of the Project B rows, just few...
So how do I get the code to go through the used range?

Any ideas would be really appreciated!
Thank you in advance!!

Here is the code:

Sub testforupdate()

Dim inputproj As String
Dim r As Range
Dim i As Integer

lastcell = Sheets("Data").UsedRange.End(xlUp).Row

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

inputproj = Application.InputBox("Please enter the Project Name:", "Project selection")
Set r = Sheets("Data").Range("BL2:BL500")

For i = 0 To lastcell

For Each r In Intersect(Range("BL2:BL500"), ActiveSheet.UsedRange)
If r.Value <> inputproj Then
r.EntireRow.delete
End If
Next r

Next i

With Application
.ScreenUpdating = True
.EnableEvents = True
End With

End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Try

Code:
Sub testforupdate()

Dim inputproj As String
Dim LR As Long, i As Long

With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With
inputproj = Application.InputBox("Please enter the Project Name:", "Project selection")
With Sheets("Data")
    LR = .Range("BL" & Rows.Count).End(xlUp).Row
    For i = LR To 2 Step -1
        If .Range("BL" & i).Value <> inputproj Then .Rows(i).Delete
    Next i
End With
With Application
    .ScreenUpdating = True
    .EnableEvents = True
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,525
Messages
6,120,051
Members
448,940
Latest member
mdusw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top