PC Review


Reply
Thread Tools Rate Thread

Accessing individual items in an array returned by a function

 
 
tkpmep@hotmail.com
Guest
Posts: n/a
 
      4th Dec 2007
In VBA, I create the following function that returns an array of
length 3.

Public Function RegionSizeStyle() As Variant
Dim results(2) As Variant
Region = "JP"
Size = "Small"
Style = "Core"

results(0) = Region
results(1) = Size
results(2) = Style

RegionSizeStyle = results

End Function

In Excel, if I select three cells in the same row, enter
=RegionSizeStyle() into the first one and then hit CTRL-SHIFT-ENTER,
the cells are filled with RegionSizeStyle(0), RegionSizeStyle(1), and
RegionSizeStyle(2) respectively, just as I would expect.

However, if I select a SINGLE cell and then try to extract a single
item form the array that is returned by typing =RegionSizeStyle()(1),
I get a #REF regardless of whether I finish by entry by hitting ENTER
or CTRL-SHIFT-ENTER. What is the problem? Is it just my syntax or is
there a deeper problem?

Thanks in advance for your assistance.

Thomas Philips
 
Reply With Quote
 
 
 
 
Pranav Vaidya
Guest
Posts: n/a
 
      4th Dec 2007
Hi,

You need to type just RegionSizeStyle() to fetch the first value. I think
you are calling the function in a wrong way.

HTH,
--
Pranav Vaidya
VBA Developer
PN, MH-India
If you think my answer is useful, please rate this post as an ANSWER!!


"(E-Mail Removed)" wrote:

> In VBA, I create the following function that returns an array of
> length 3.
>
> Public Function RegionSizeStyle() As Variant
> Dim results(2) As Variant
> Region = "JP"
> Size = "Small"
> Style = "Core"
>
> results(0) = Region
> results(1) = Size
> results(2) = Style
>
> RegionSizeStyle = results
>
> End Function
>
> In Excel, if I select three cells in the same row, enter
> =RegionSizeStyle() into the first one and then hit CTRL-SHIFT-ENTER,
> the cells are filled with RegionSizeStyle(0), RegionSizeStyle(1), and
> RegionSizeStyle(2) respectively, just as I would expect.
>
> However, if I select a SINGLE cell and then try to extract a single
> item form the array that is returned by typing =RegionSizeStyle()(1),
> I get a #REF regardless of whether I finish by entry by hitting ENTER
> or CTRL-SHIFT-ENTER. What is the problem? Is it just my syntax or is
> there a deeper problem?
>
> Thanks in advance for your assistance.
>
> Thomas Philips
>

 
Reply With Quote
 
tkpmep@hotmail.com
Guest
Posts: n/a
 
      4th Dec 2007
Got it by modifying the keywords in my Google search - The correct
syntax is
=Index(RegionSizeStyle(), i)
for i=1, 2 or 3.


 
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
Accessing individual elements of an array returned by a function tkpmep@hotmail.com Microsoft Excel Programming 1 19th Mar 2008 06:13 AM
array results not returned =?Utf-8?B?SlJNLWRyb2Y=?= Microsoft Excel Worksheet Functions 2 27th Jun 2006 06:46 PM
Accessing Array Items after Response.Redirect =?Utf-8?B?U3RlcGhlbg==?= Microsoft Dot NET 11 29th Oct 2004 12:44 PM
How to access an array of characters returned by a function in dll? =?Utf-8?B?amFja3NyZWU=?= Microsoft C# .NET 2 23rd Mar 2004 09:59 AM
Preformatted array returned by custom function Asif Microsoft Excel Programming 9 4th Dec 2003 06:31 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:26 PM.