Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default HELP: Excel Interop objects and checking for nulls?

hi
I've been having this trouble for a long time:

Is there any way I can check if a reference to a range object no longer
exists?

Eg, After creating a reference to a range:

Range refRange = Application.ActiveCell;

I delete the cell (eg, via removing an entire row/column), yet the refRange
object still points to the deleted range object. When I try to use it, I get
an exception.

This problem persists with any type of Excel Interop objects (Name, Names,
Range, etc).

Was wondering if anyone know if I can check if an Excel Interop object is
usable? Eg, checking if it's null or not.

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Excel Interop objects and checking for nulls?

try
x = refRange.areas(1).address

if that errors it has been entirely deleted, assuming 'Not refRange Is
Nothing'

Not sure if relevant for you but keep in mind also the range may have been
moved (within the sheet) since it was created, and still be valid.

Regards,
Peter T

"JsjsLim" wrote in message
...
hi
I've been having this trouble for a long time:

Is there any way I can check if a reference to a range object no longer
exists?

Eg, After creating a reference to a range:

Range refRange = Application.ActiveCell;

I delete the cell (eg, via removing an entire row/column), yet the
refRange
object still points to the deleted range object. When I try to use it, I
get
an exception.

This problem persists with any type of Excel Interop objects (Name, Names,
Range, etc).

Was wondering if anyone know if I can check if an Excel Interop object is
usable? Eg, checking if it's null or not.

Thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Excel Interop objects and checking for nulls?

Hi Peter,

I'm aware that sometimes, a range might have been moved, and that's fine,
because I can still go about my business (eg, range.text, or range.formula,
etc).

However, my problem occurs when a range is no longer valid (deleted). Any
references that I have will still point to the invalid range. And when I try
to access any of the attributes (once again like above, range.text,
range.formula, etc), it throws an exception (0x800A01A8 I believe), which is
what one would expect.

What I would like to know, however, is if it's possible to check if a
reference to an Excel range is still valid or not. Something like range ==
null? or perhaps is there a IsDisposed attribute within the range object that
I can check? Currently, I'm handling this via try-catch, which imho is a
really inefficient way of doing things.

I'd rather have code that looks like this:

if (!range.IsDisposed) { ...formula = range.Formula... }
or
if (range != null) { ...formula = range.Formula... }

instead of
try { ...formula = range.Formula... }
catch{ // Range is in a faulted state and can't be used }

Thanks




"Peter T" wrote:

try
x = refRange.areas(1).address

if that errors it has been entirely deleted, assuming 'Not refRange Is
Nothing'

Not sure if relevant for you but keep in mind also the range may have been
moved (within the sheet) since it was created, and still be valid.

Regards,
Peter T

"JsjsLim" wrote in message
...
hi
I've been having this trouble for a long time:

Is there any way I can check if a reference to a range object no longer
exists?

Eg, After creating a reference to a range:

Range refRange = Application.ActiveCell;

I delete the cell (eg, via removing an entire row/column), yet the
refRange
object still points to the deleted range object. When I try to use it, I
get
an exception.

This problem persists with any type of Excel Interop objects (Name, Names,
Range, etc).

Was wondering if anyone know if I can check if an Excel Interop object is
usable? Eg, checking if it's null or not.

Thanks




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Excel Interop objects and checking for nulls?

AFAIK there is no other way to directly test if a referenced range has been
"removed" from the sheet other than to attempt to refer to one of its
properties.

Normally your routine will have some sort of error handler. Typically you
can proceed on the assumption the range still exists, but if the code fails
the first thing to test would be the integrity of the actual range along the
lines you posted.

A way to test without invoking any error would be to "name" the range or
refer to the range in some other (hidden) cell formula, eg (VBA)

Dim r As Range
Set r = Range("a1")
r.Name = "nmCell"
Rows(1).Delete
If InStr(2, ActiveWorkbook.Names("nmcell").RefersTo, "#REF!") Then
MsgBox "range removed"
End If


FWIW, I use hidden worksheet level names to be able to reconstruct same
range ref after file save, close, re-open.

Regards,
Peter T



"JsjsLim" wrote in message
...
Hi Peter,

I'm aware that sometimes, a range might have been moved, and that's fine,
because I can still go about my business (eg, range.text, or
range.formula,
etc).

However, my problem occurs when a range is no longer valid (deleted). Any
references that I have will still point to the invalid range. And when I
try
to access any of the attributes (once again like above, range.text,
range.formula, etc), it throws an exception (0x800A01A8 I believe), which
is
what one would expect.

What I would like to know, however, is if it's possible to check if a
reference to an Excel range is still valid or not. Something like range ==
null? or perhaps is there a IsDisposed attribute within the range object
that
I can check? Currently, I'm handling this via try-catch, which imho is a
really inefficient way of doing things.

I'd rather have code that looks like this:

if (!range.IsDisposed) { ...formula = range.Formula... }
or
if (range != null) { ...formula = range.Formula... }

instead of
try { ...formula = range.Formula... }
catch{ // Range is in a faulted state and can't be used }

Thanks




"Peter T" wrote:

try
x = refRange.areas(1).address

if that errors it has been entirely deleted, assuming 'Not refRange Is
Nothing'

Not sure if relevant for you but keep in mind also the range may have
been
moved (within the sheet) since it was created, and still be valid.

Regards,
Peter T

"JsjsLim" wrote in message
...
hi
I've been having this trouble for a long time:

Is there any way I can check if a reference to a range object no longer
exists?

Eg, After creating a reference to a range:

Range refRange = Application.ActiveCell;

I delete the cell (eg, via removing an entire row/column), yet the
refRange
object still points to the deleted range object. When I try to use it,
I
get
an exception.

This problem persists with any type of Excel Interop objects (Name,
Names,
Range, etc).

Was wondering if anyone know if I can check if an Excel Interop object
is
usable? Eg, checking if it's null or not.

Thanks






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Excel Interop objects and checking for nulls?

Hi Peter,

Sorry for the late reply, and thanks for the help. Not exactly what I
needed, but I must admit that it's a smart solution.

Thanks again!



"Peter T" wrote:

AFAIK there is no other way to directly test if a referenced range has been
"removed" from the sheet other than to attempt to refer to one of its
properties.

Normally your routine will have some sort of error handler. Typically you
can proceed on the assumption the range still exists, but if the code fails
the first thing to test would be the integrity of the actual range along the
lines you posted.

A way to test without invoking any error would be to "name" the range or
refer to the range in some other (hidden) cell formula, eg (VBA)

Dim r As Range
Set r = Range("a1")
r.Name = "nmCell"
Rows(1).Delete
If InStr(2, ActiveWorkbook.Names("nmcell").RefersTo, "#REF!") Then
MsgBox "range removed"
End If


FWIW, I use hidden worksheet level names to be able to reconstruct same
range ref after file save, close, re-open.

Regards,
Peter T



"JsjsLim" wrote in message
...
Hi Peter,

I'm aware that sometimes, a range might have been moved, and that's fine,
because I can still go about my business (eg, range.text, or
range.formula,
etc).

However, my problem occurs when a range is no longer valid (deleted). Any
references that I have will still point to the invalid range. And when I
try
to access any of the attributes (once again like above, range.text,
range.formula, etc), it throws an exception (0x800A01A8 I believe), which
is
what one would expect.

What I would like to know, however, is if it's possible to check if a
reference to an Excel range is still valid or not. Something like range ==
null? or perhaps is there a IsDisposed attribute within the range object
that
I can check? Currently, I'm handling this via try-catch, which imho is a
really inefficient way of doing things.

I'd rather have code that looks like this:

if (!range.IsDisposed) { ...formula = range.Formula... }
or
if (range != null) { ...formula = range.Formula... }

instead of
try { ...formula = range.Formula... }
catch{ // Range is in a faulted state and can't be used }

Thanks




"Peter T" wrote:

try
x = refRange.areas(1).address

if that errors it has been entirely deleted, assuming 'Not refRange Is
Nothing'

Not sure if relevant for you but keep in mind also the range may have
been
moved (within the sheet) since it was created, and still be valid.

Regards,
Peter T

"JsjsLim" wrote in message
...
hi
I've been having this trouble for a long time:

Is there any way I can check if a reference to a range object no longer
exists?

Eg, After creating a reference to a range:

Range refRange = Application.ActiveCell;

I delete the cell (eg, via removing an entire row/column), yet the
refRange
object still points to the deleted range object. When I try to use it,
I
get
an exception.

This problem persists with any type of Excel Interop objects (Name,
Names,
Range, etc).

Was wondering if anyone know if I can check if an Excel Interop object
is
usable? Eg, checking if it's null or not.

Thanks






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
Interop Excel thejamie Excel Programming 2 March 24th 08 11:41 AM
Excel VBA Class Objects - Parent & Successor Objects [email protected] Excel Programming 1 January 15th 07 12:06 AM
Using Excel, how do I replace cells containing blanks with nulls? Nither1 Excel Discussion (Misc queries) 2 May 9th 05 10:32 PM
How do I sort nulls within date field in Excel Petra[_2_] Excel Programming 2 April 26th 05 03:04 AM
Unable to remove Sheet objects in the Microsoft Excel Objects Adrian[_7_] Excel Programming 1 August 26th 04 10:49 PM


All times are GMT +1. The time now is 07:07 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"