Error 91 in using while loop with Cells.Find

Raj007

New Member
Joined
Sep 3, 2014
Messages
3
Hi am a basic learner in using VB. I created the following code using record macro. It does the job. However, it is giving error. Hence, I request you to take a look.

While (Cells.Find(What:="Data", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate And ActiveCell.Row <= ActiveSheet.UsedRange.Rows.Count)
Application.ScreenUpdating = True
Fnd = ActiveCell.Row
Frm = Fnd - 2
Too = Fnd + 5
Rows(Frm & ":" & Too).Select
Selection.Delete Shift:=xlUp
Cells(Frm, 1).Select
Wend

Appreciate your response. Thanks!
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
The criteria for the While loop isn't right.

What is your code meant to do?

What part of it is recorded?
 
Upvote 0
The criteria for the While loop isn't right.

What is your code meant to do?

What part of it is recorded?

Objective:
The code has to search for the name "Data"(in the first column). After it finds the cell that has "Data" in the i'th row, it selects rows from i-2 to i+5 and deletes the same.
 
Upvote 0
Try this.
Code:
For I = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1

    Set rng = Range("A" & I)

    If rng.Value = "Data" Then
        rng.Offset(-2).Resize(8).Delete xlShiftUp
    End If

Next I
 
Upvote 0
Hi Norie,

Thanks!
It worked fine. However it is slow as I have more than 200000 lines of data in each file.

Thanks alot.... :)
 
Upvote 0

Forum statistics

Threads
1,206,760
Messages
6,074,779
Members
446,087
Latest member
PinkFloyd

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