Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 106
Default Range existence check

I need to delete a range and re-create it later in my code. I tried the
following but it doesn't work if the range does not exist:

On Error Resume Next
Range("D_CARDS").Delete

How can I check for the existence of a range and, in case it does exist,
delete it, otherwise continue with my code?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Range existence check

I am not sure about the validity of your statement that it doesn't work, but
if you want to be more verbose:

Dim rng as Range
Set rng = Nothing
On error Resume Next
set rng = Range("D_Cards")
On error goto 0
if not rng is nothing then
rng.Delete
end if

--
Regards,
Tom Ogilvy

"DoctorG" wrote:

I need to delete a range and re-create it later in my code. I tried the
following but it doesn't work if the range does not exist:

On Error Resume Next
Range("D_CARDS").Delete

How can I check for the existence of a range and, in case it does exist,
delete it, otherwise continue with my code?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Range existence check

If it doesn't exist, how do you know it didn't work?

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"DoctorG" wrote in message
...
I need to delete a range and re-create it later in my code. I tried the
following but it doesn't work if the range does not exist:

On Error Resume Next
Range("D_CARDS").Delete

How can I check for the existence of a range and, in case it does exist,
delete it, otherwise continue with my code?



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 106
Default Range existence check

Tom I get the same response from Excel as before :

Run-time error 1004
Method 'Range' of object '_Global' failed

Can you guess what might be wrong? I guess that's why you weren't sure about
the validity of my previous statement. Something else is probably causing
this behaviour.

"Tom Ogilvy" wrote:

I am not sure about the validity of your statement that it doesn't work, but
if you want to be more verbose:

Dim rng as Range
Set rng = Nothing
On error Resume Next
set rng = Range("D_Cards")
On error goto 0
if not rng is nothing then
rng.Delete
end if

--
Regards,
Tom Ogilvy

"DoctorG" wrote:

I need to delete a range and re-create it later in my code. I tried the
following but it doesn't work if the range does not exist:

On Error Resume Next
Range("D_CARDS").Delete

How can I check for the existence of a range and, in case it does exist,
delete it, otherwise continue with my code?

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 106
Default Range existence check

I get the 1004 error message when Excel processes either of the following
statements:

Range("D_CARDS").Delete
or
set rng = RANGE("D_CARDS")

"Bob Phillips" wrote:

If it doesn't exist, how do you know it didn't work?

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"DoctorG" wrote in message
...
I need to delete a range and re-create it later in my code. I tried the
following but it doesn't work if the range does not exist:

On Error Resume Next
Range("D_CARDS").Delete

How can I check for the existence of a range and, in case it does exist,
delete it, otherwise continue with my code?






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Range existence check

I pasted the code in a blank sheet/new workbook and ran it with no problem.
the Error handler will suppress a 1004 error in either my code or your
original. Wherever the error is coming from, it is not in the region covered
by
On Error Resume Next


I also don't know what you want to achieve by doing Range("D_Cards").Delete

when you do that, D_Cards continues to exist as a named range, but it has a
refers to argument like =Sheet1!#REf

If you want to clear the data in that range, use

Range("D_Cards").Clear
if you want to remove the name
Thisworkbook.Names("D_Cards").Delete

if you want to do both
Range("D_Cards").Clear
Thisworkbook.Names("D_Cards").Delete

--
Regards,
Tom Ogilvy


"DoctorG" wrote:

Tom I get the same response from Excel as before :

Run-time error 1004
Method 'Range' of object '_Global' failed

Can you guess what might be wrong? I guess that's why you weren't sure about
the validity of my previous statement. Something else is probably causing
this behaviour.

"Tom Ogilvy" wrote:

I am not sure about the validity of your statement that it doesn't work, but
if you want to be more verbose:

Dim rng as Range
Set rng = Nothing
On error Resume Next
set rng = Range("D_Cards")
On error goto 0
if not rng is nothing then
rng.Delete
end if

--
Regards,
Tom Ogilvy

"DoctorG" wrote:

I need to delete a range and re-create it later in my code. I tried the
following but it doesn't work if the range does not exist:

On Error Resume Next
Range("D_CARDS").Delete

How can I check for the existence of a range and, in case it does exist,
delete it, otherwise continue with my code?

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 106
Default Range existence check

You are right on the bad reference the Range("D_CARDS").Delete command
causes. Perhaps this is the reason the 1004 appears in the first place.

Anyway you covered all options in your answer, along with what I need to do,
so ... Thanks a lot!! Case closed.

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
Check for existence CWillis Excel Discussion (Misc queries) 3 May 31st 06 01:20 PM
VBA to check for existence of a DSN GPO Excel Programming 2 May 29th 06 05:22 AM
Check File Existence Catalin[_2_] Excel Programming 5 May 10th 06 10:20 AM
How do I check for existence of a worksheet? LizzieHW Excel Worksheet Functions 1 July 19th 05 06:22 PM
Code to check existence No Name Excel Programming 2 November 4th 04 01:50 PM


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

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"