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

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
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
 

thamestowers

New Member
Joined
Jun 8, 2014
Messages
23
holy mother of codes!!!
this one worked perfectly!
a huge bow of gratitude!
thank you very much! :D
 

Forum statistics

Threads
1,136,210
Messages
5,674,420
Members
419,508
Latest member
trinstrick

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
Top