Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default To to check whether a string is a valid reference

Hiya

I wish to take a string such as

ThisString = "'Sheet Name'!$A$1:$C$3"

And use it within a range object like so:

Set Location = Range(ThisString)

This example would work but I wish to avoid errors generated by strings
which aren't valid references.

Is there a method about which can check whether the string is a valid
reference?

I'm not too hot on error trapping and generally aim to avoid them in the
first place. So I would prefer not to use the On Error statement unless you
thoroughly explain it as if I'm very stupid.

TIA!

Nick
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default To to check whether a string is a valid reference

Hi Nick,

I wish to take a string such as

ThisString = "'Sheet Name'!$A$1:$C$3"

Is there a method about which can check whether the string is a valid
reference?


on error resume next
Set Location = Range(ThisString)
on error goto 0
if location is nothing then
msgbox "error"
exit sub
end if


Frank's code should work fine.

Just to follow up on the second part of your post:

I'm not too hot on error trapping and generally aim to avoid them in
the first place. So I would prefer not to use the On Error statement
unless you
thoroughly explain it as if I'm very stupid.


There's nothing wrong with using error handling in situations like this.
Since Frank used On Error Resume Next, the code will continue to the next
line even if a runtime error is encountered. So if the reference is
invalid, the Location object variable will not be set to a valid range, and
Location will be equal to Nothing.

Here's Frank's example wrapped in a function:

Public Function gbIsValidRange(rsAddress As String) As Boolean
On Error Resume Next
gbIsValidRange = Range(rsAddress).Row
On Error GoTo 0
End Function

Here's another function that shows how you could do it without error
handling:

Public Function gbIsValidRange2(rsAddress As String) As Boolean
gbIsValidRange2 = (StrComp(TypeName(Evaluate(rsAddress)), _
"range", vbTextCompare) = 0)
End Function

But I would recommend the first method, as it executes in about half the
time of the second.

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]

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
Zero-Length string vs Zero (also a valid value) [email protected] Excel Worksheet Functions 2 January 24th 09 02:18 PM
How to check valid Date value? hstijnen Excel Worksheet Functions 1 August 14th 06 01:25 PM
Valid SQL string Malcolm Agingwell Excel Discussion (Misc queries) 0 August 12th 05 11:00 AM
How to check that a string is a valid formula ? Adrian[_7_] Excel Programming 5 July 30th 04 07:10 PM
check for valid file pabs[_21_] Excel Programming 2 January 15th 04 06:52 AM


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