Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Need VBA code to terminate a find loop


When I import a report & run the following VBA code to delete the
headings, after the last heading is deleted, I get:

Runtime Error '91": Object Variable or With block variable not
set

What VBA code should be used to terminate the loop to prevent the
error?

mikeburg


Sub CDeleteHeadings()
Cells.find(what:="Name & Address", After:=ActiveCell,
LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows,
SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Do
ActiveCell.Offset(rowoffset:=0).EntireRow.Delete
ActiveCell.Offset(rowoffset:=0).EntireRow.Delete
Cells.findnext(After:=ActiveCell).Activate
Loop Until Cells.findnext = False


--
mikeburg
------------------------------------------------------------------------
mikeburg's Profile: http://www.excelforum.com/member.php...o&userid=24581
View this thread: http://www.excelforum.com/showthread...hreadid=381737

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default Need VBA code to terminate a find loop

You can try modifying your code by changing
Loop Until Cells.findnext = False
to
Loop Until Cells.findnext is nothing
(untested)
or try this code which should be more efficient...

Sub Test
Call DeleteUnwanted("Name & Address")
End Sub

Sub DeleteUnwanted(ByVal DeleteWord As String)
Dim wks As Worksheet
Dim rngToSearch As Range
Dim rngCurrent As Range
Dim rngDelete As Range
Dim rngFirst As Range

Set wks = Sheets("Sheet1")
Set rngToSearch = wks.Cells

Set rngCurrent = rngToSearch.Find(DeleteWord)
If Not rngCurrent Is Nothing Then
Set rngDelete = rngCurrent
Set rngFirst = rngCurrent
Do
Set rngDelete = Union(rngCurrent, rngCurrent.Offset(1,0),
rngDelete)
Set rngCurrent = rngToSearch.FindNext(rngCurrent)
Loop Until rngCurrent.Address = rngFirst.Address
rngDelete.EntireRow.Delete
End If
End Sub

--
HTH...

Jim Thomlinson


"mikeburg" wrote:


When I import a report & run the following VBA code to delete the
headings, after the last heading is deleted, I get:

Runtime Error '91": Object Variable or With block variable not
set

What VBA code should be used to terminate the loop to prevent the
error?

mikeburg


Sub CDeleteHeadings()
Cells.find(what:="Name & Address", After:=ActiveCell,
LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows,
SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Do
ActiveCell.Offset(rowoffset:=0).EntireRow.Delete
ActiveCell.Offset(rowoffset:=0).EntireRow.Delete
Cells.findnext(After:=ActiveCell).Activate
Loop Until Cells.findnext = False


--
mikeburg
------------------------------------------------------------------------
mikeburg's Profile: http://www.excelforum.com/member.php...o&userid=24581
View this thread: http://www.excelforum.com/showthread...hreadid=381737


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Need VBA code to terminate a find loop


Rearrange some things and test for Is Nothing rather than False and
you've got it.

Sub CDeleteHeadings()
Do
Cells.Find(what:="Name & Address", After:=ActiveCell,
LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(rowoffset:=0).EntireRow.Delete
ActiveCell.Offset(rowoffset:=0).EntireRow.Delete
Loop Until Cells.FindNext Is Nothing
End Sub

HTH


--
bhofsetz
------------------------------------------------------------------------
bhofsetz's Profile: http://www.excelforum.com/member.php...o&userid=18807
View this thread: http://www.excelforum.com/showthread...hreadid=381737

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop or Find code ?? need help !! EBnLP01 Excel Worksheet Functions 4 October 29th 09 05:47 PM
Find loop doesn't loop JSnow Excel Discussion (Misc queries) 2 June 24th 09 08:28 PM
How to Loop some code Phil Osman Excel Discussion (Misc queries) 2 August 19th 05 11:14 AM
VBE code in a loop loloflores Excel Programming 0 April 30th 04 12:28 PM
code to terminate access after certain time bruce forster Excel Programming 4 April 22nd 04 11:57 PM


All times are GMT +1. The time now is 10:50 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"