Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / December 2007

Tip: Looking for answers? Try searching our database.

findnext error in loop

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SLW612 - 11 Dec 2007 15:50 GMT
Hi everyone,
First of all thanks to everyone who has helped me so far.

I have one little quirk in my code and I'm not sure how to fix it.  The
following code works fine if there is more than one instance of "Account
Category:*" found in col. K:T, but if there is only one instance found, I get
the error message "Run-time error '91': Object variable or With block
variable not set."

Help!  Thank you!

   Dim rngFound As Range
   Dim rngToSearch As Range
   Dim strFirstAddress As String
       Dim wks As Worksheet
   
   Set wks = ActiveSheet
   
   With wks
   
   Set rngToSearch = Sheets("Sheet1").Columns("K:T")
   Set rngFound = rngToSearch.Find(What:="Account Category:*", _
                                   LookAt:=xlWhole, _
                                   LookIn:=xlValues, _
                                   MatchCase:=False)
   If Not rngFound Is Nothing Then
       strFirstAddress = rngFound.Address
       
       Do
           rngFound.Select
               Call ThisWorks2 '<-this macro only formats the cell
           Set rngFound = rngToSearch.FindNext(rngFound)
       Loop Until rngFound Is Nothing Or strFirstAddress = rngFound.Address
   Else
   End If
End With
SLW612 - 11 Dec 2007 16:06 GMT
Never mind, I just found it through some more searching in the forums:
Just added "if rngfound is nothing then exit do" right before the loop
command.

> Hi everyone,
> First of all thanks to everyone who has helped me so far.
[quoted text clipped - 32 lines]
>     End If
> End With
JE McGimpsey - 11 Dec 2007 16:07 GMT
This works fine for me.

Are you sure ThisWorks2 only formats the cell?

> Hi everyone,
> First of all thanks to everyone who has helped me so far.
[quoted text clipped - 32 lines]
>     End If
> End With
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.