Loop Macro

M

Mikey

I created a simple Macro to Find and then delete certain rows.

Cells.Find(What:="Page", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Rows("1:1").EntireRow.Select
Selection.Delete Shift:=xlUp

End Sub

I them wanted to make this macro loop through the column until all instances
of Page were deleted. So I added Do Until ActiveCell.Value = " " at the
beginning and Loop before the End Sub. It removed all the rows but a run-time
error '91' appeared that stated Object variable or with block variable not
set. How do I correct my loop statement to run correctly?
 
P

p45cal

Mikey;551139 said:
I created a simple Macro to Find and then delete certain rows.

Cells.Find(What:="Page", After:=ActiveCell, LookIn:=xlFormulas, LookA
_
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Rows("1:1").EntireRow.Select
Selection.Delete Shift:=xlUp

End Sub

I them wanted to make this macro loop through the column until al
instances
of Page were deleted. So I added Do Until ActiveCell.Value = " " a
the
beginning and Loop before the End Sub. It removed all the rows but
run-time
error '91' appeared that stated Object variable or with block variabl
not
set. How do I correct my loop statement to run correctly?


Do
Set xx = Cells.Find(What:="Page", LookIn:=xlFormulas, LookAt:=xlPart
MatchCase:=False, SearchFormat:=False)
If Not xx Is Nothing Then xx.EntireRow.Delete
Loop Until xx Is Nothing
Note that this code and yours does't restrict itself t
just searching a column, but the whole sheet
 
D

Dave Peterson

I'd try:

Dim FoundCell as range
dim wks as worksheet

set wks = worksheets("Sheet1")

do
with wks.range("a1").entirecolumn 'What column???
set foundcell = .cells.find(what:="Page", _
after:=.cells(.cells.count), _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
end with

if foundcell is nothing then
exit do
else
foundcell.entirerow.delete
end if
Loop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top