Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 201
Default How to set variable to nothing

Excel 2002, WinXP
I have the following code within a loop.

On Error Resume Next
Sht = ClientList.Find(What:=.............
On Error GoTo 0
If Sht Is Nothing Then.................

My problem is that, because this in a loop, the variable "Sht" will retain
its previous value if the Find statement cannot find the search item.
Therefore, the "If Sht Is Nothing Then....." statement will not do what I
want.

My question is:
What can I set as the value of Sht, before the above code, to have the "If"
statement react as I want? Sht = Nothing doesn't do it.

Or a more general question:
How can I determine that the Find statement found nothing?

I know that I can use "On Error GoTo Somewhere" but is there a better way?
Thanks for your help. Otto


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default How to set variable to nothing

Only object variables can have a value of Nothing. One way:

Dim rFound As Range
On Error Resume Next
Set rFound = ClientList.Find(...
On Error GoTo 0
If rFound Is Nothing Then
MsgBox "Not Found"
Else
'Do something else
End If


In article ,
"Otto Moehrbach" wrote:

Excel 2002, WinXP
I have the following code within a loop.

On Error Resume Next
Sht = ClientList.Find(What:=.............
On Error GoTo 0
If Sht Is Nothing Then.................

My problem is that, because this in a loop, the variable "Sht" will retain
its previous value if the Find statement cannot find the search item.
Therefore, the "If Sht Is Nothing Then....." statement will not do what I
want.

My question is:
What can I set as the value of Sht, before the above code, to have the "If"
statement react as I want? Sht = Nothing doesn't do it.

Or a more general question:
How can I determine that the Find statement found nothing?

I know that I can use "On Error GoTo Somewhere" but is there a better way?
Thanks for your help. Otto

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default How to set variable to nothing

set sht = nothing
(simpler than you thought, huh?)

or:

dim FoundCell as range

set foundcell = worksheets("sheet1").cells.find(....)

if foundcell is nothing then
'it's not found
else
msgbox foundcell.address
end if

You don't need the on error stuff and for the .find, you don't need to set it to
nothing first (but it won't hurt too much--I've been corrected a few times for
having that in code I've posted to the ng. <bg).


Otto Moehrbach wrote:

Excel 2002, WinXP
I have the following code within a loop.

On Error Resume Next
Sht = ClientList.Find(What:=.............
On Error GoTo 0
If Sht Is Nothing Then.................

My problem is that, because this in a loop, the variable "Sht" will retain
its previous value if the Find statement cannot find the search item.
Therefore, the "If Sht Is Nothing Then....." statement will not do what I
want.

My question is:
What can I set as the value of Sht, before the above code, to have the "If"
statement react as I want? Sht = Nothing doesn't do it.

Or a more general question:
How can I determine that the Find statement found nothing?

I know that I can use "On Error GoTo Somewhere" but is there a better way?
Thanks for your help. Otto


--

Dave Peterson

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default How to set variable to nothing

Otto,

As an object variable, you Set it, both to it's initial value and to reset
to nothing.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Otto Moehrbach" wrote in message
...
Excel 2002, WinXP
I have the following code within a loop.

On Error Resume Next
Sht = ClientList.Find(What:=.............
On Error GoTo 0
If Sht Is Nothing Then.................

My problem is that, because this in a loop, the variable "Sht" will retain
its previous value if the Find statement cannot find the search item.
Therefore, the "If Sht Is Nothing Then....." statement will not do what I
want.

My question is:
What can I set as the value of Sht, before the above code, to have the

"If"
statement react as I want? Sht = Nothing doesn't do it.

Or a more general question:
How can I determine that the Find statement found nothing?

I know that I can use "On Error GoTo Somewhere" but is there a better way?
Thanks for your help. Otto




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 201
Default How to set variable to nothing

JE, Dave, Bob
Thanks for the help. Otto
"Otto Moehrbach" wrote in message
...
Excel 2002, WinXP
I have the following code within a loop.

On Error Resume Next
Sht = ClientList.Find(What:=.............
On Error GoTo 0
If Sht Is Nothing Then.................

My problem is that, because this in a loop, the variable "Sht" will retain
its previous value if the Find statement cannot find the search item.
Therefore, the "If Sht Is Nothing Then....." statement will not do what I
want.

My question is:
What can I set as the value of Sht, before the above code, to have the

"If"
statement react as I want? Sht = Nothing doesn't do it.

Or a more general question:
How can I determine that the Find statement found nothing?

I know that I can use "On Error GoTo Somewhere" but is there a better way?
Thanks for your help. Otto






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
Runtime Error '91' Object variable or With block variable not set Alec Coliver Excel Discussion (Misc queries) 2 October 24th 09 02:29 PM
variable height variable width stacked bar charts ambthiru Charts and Charting in Excel 3 January 18th 06 11:41 PM
why is it saying sheetcnt is "variable not defined" how to do a global variable to share over multiple functions in vba for excel? Daniel Excel Worksheet Functions 1 July 9th 05 03:05 AM
Cells.Find error Object variable or With block variable not set Peter[_21_] Excel Programming 2 May 8th 04 02:15 PM
Pivot Table - Object variable or with block variable not set? George Nicholson[_2_] Excel Programming 1 April 16th 04 09:12 PM


All times are GMT +1. The time now is 03:21 PM.

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"