PC Review


Reply
Thread Tools Rate Thread

Determine if variable contains a valid range reference?

 
 
ker_01
Guest
Posts: n/a
 
      29th Jul 2009
I'm trying to create a function (UDF) that will allow input two ranges; one
is required, the other is optional. See sample code below.

If the required range isn't included, the function returns an error, which
is fine. However, when only the optional range is omitted, my code blows up
(without an error) when I try to process the returned value. I've done some
searching, but haven't found a way to easily/cleanly determine if the
variable returned by the function is a valid range (so that I can
conditionally process those values).

Can anyone point me in the right direction?
Many thanks!
Keith

much-simplified sample below (with two problems); one is that when there is
no MyOptionalRange entered, the code never reaches the msgbox (I'd at least
have expected an error). The other problem is that when MyOptionalRange /is/
included and the values are being entered via the formula entry pop-up (and
not entered by hand directly in the cell), the code seems to loop more than
once (msgbox keeps popping up) and I'm not sure why or how to ensure that it
only processes once, when the formula box is closed.

Function ThisIsATest(MyRequiredRange As Range, MyOptionalRange As Range)

Dim MyRequiredVariantArray As Variant
MyRequiredVariantArray = MyRequiredRange.Value
Debug.Print MyRequiredVariantArray(1, 1)

Dim MyOptionalVariantArray As Variant
MyOptionalVariantArray = MyOptionalRange.Value
Debug.Print MyOptionalVariantArray(1, 1)

MsgBox "All Done"

End Function

 
Reply With Quote
 
 
 
 
OssieMac
Guest
Posts: n/a
 
      30th Jul 2009
If the range is optional then you need to include Optional in the Function so
that it does not return an error. You then test for its value in the code.

Function ThisIsATest(MyRequiredRange As Range, _
Optional MyOptionalRange As Range)

If MyOptionalRange Is Nothing Then
'code for optional range not included
MsgBox "MyOptionalRange is Nothing"
Else
'code if included
MsgBox "MyOptionalRange is: " _
& MyOptionalRange.Address
End If


'Alternatively

If Not MyOptionalRange Is Nothing Then
'code here becaue it is included
MsgBox "MyOptionalRange is: " _
& MyOptionalRange.Address
Else
'code for optional range not included
MsgBox "MyOptionalRange is Nothing"
End If
--
Regards,

OssieMac


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Range reference is not valid Basta1980 Microsoft Excel Programming 0 15th Oct 2009 05:29 PM
Conflict with Valid Range Reference Error in Microsoft Excel 2007 Vamsi Microsoft Excel Programming 4 3rd Mar 2009 01:29 PM
The name ... either conflicts with a valid range reference or isinvalid for Excel. Greg Lovern Microsoft Excel Programming 3 2nd Apr 2008 06:52 PM
Conflict with valid range reference error =?Utf-8?B?c21hcnV6emk=?= Microsoft Excel Misc 1 14th Mar 2007 10:05 PM
Input cell reference is not valid (One Variable Data Table) =?Utf-8?B?RG90dG9yZQ==?= Microsoft Excel Worksheet Functions 9 1st Sep 2005 03:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:53 PM.