Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default findnext error in loop

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default findnext error in loop

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.

"SLW612" wrote:

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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default findnext error in loop

This works fine for me.

Are you sure ThisWorks2 only formats the cell?



In article ,
SLW612 wrote:

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

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
Help with FindNext error SLW612 Excel Programming 3 December 10th 07 04:00 PM
Do loop error while using FindNext command Adam Excel Programming 0 June 29th 06 06:44 PM
loop and findnext John Excel Programming 4 August 25th 05 03:21 PM
Find, Findnext VBA Loop SMS - John Howard Excel Programming 5 November 13th 04 03:19 AM
Run-time Error '1004' Unable to get the findnext property... Tom Ogilvy Excel Programming 0 July 12th 04 04:27 PM


All times are GMT +1. The time now is 12:04 AM.

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

About Us

"It's about Microsoft Excel"