PC Review


Reply
Thread Tools Rate Thread

Assigning a Range to a dynamic array

 
 
Tommy
Guest
Posts: n/a
 
      20th Feb 2008
Hey guys,

I have a function that I would like to use to return an array of
ranges. It is important that the returned array however is dynamic as
I would then like to be able to use a loop to union the ranges
regardless of how many there are. Here is the code so far:

Public Function SelectSeries(SeriesColumnLetter) As Variant()

Sheets("Data").Select

Dim SeriesRange(1 To 4)
Dim DataSelection(1 To 4)
Dim DataSource() As Variant

Dim i

For i = 1 To 4

If SeriesColumnLetter(i) <> " " Then

Set SeriesRange(i) = Worksheets("Data").Range(SeriesColumnLetter(i) &
"7:" & SeriesColumnLetter(i) & "65536")
DataNumber = Application.WorksheetFunction.CountA(SeriesRange(i))
DataSelection(i) = SeriesColumnLetter(i) & "7:" &
SeriesColumnLetter(i) & (DataNumber + 6)

Set DataSource(i) = Worksheets("Data").Range(DataSelection(i))

Else

MsgBox ("No column letter!")

End If

Next

SelectSeries = DataSource()

End Function

Four SeriesColumnLetter variables are input into the function.Usually
these values are a letter, but sometimes they are blank. If they are
blank, I would like them to be excluded from the returning array i.e.
if 1 out of the 4 is blank, then the returning array would contain 3
values.

At the line:

Set DataSource(i) = Worksheets("Data").Range(DataSelection(i))

I get the error: 'Subscript out of range'. I guess this is because the
array has been declared dynamically. How can I get around this and
return a dynamic array??

Thanks and regards,

Tom
 
Reply With Quote
 
 
 
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      20th Feb 2008
I've tried reading your code over a few times, but I am having trouble
following what you are trying to do. Forget about the code (for now), can
you explain in words what your data looks like, what you are passing into
the function (you seem to be processing it as an array, is it?) and what
"ranges" you want the function to pass back?

Rick


"Tommy" <(E-Mail Removed)> wrote in message
news:159e70a4-abb5-4348-8b2e-(E-Mail Removed)...
> Hey guys,
>
> I have a function that I would like to use to return an array of
> ranges. It is important that the returned array however is dynamic as
> I would then like to be able to use a loop to union the ranges
> regardless of how many there are. Here is the code so far:
>
> Public Function SelectSeries(SeriesColumnLetter) As Variant()
>
> Sheets("Data").Select
>
> Dim SeriesRange(1 To 4)
> Dim DataSelection(1 To 4)
> Dim DataSource() As Variant
>
> Dim i
>
> For i = 1 To 4
>
> If SeriesColumnLetter(i) <> " " Then
>
> Set SeriesRange(i) = Worksheets("Data").Range(SeriesColumnLetter(i) &
> "7:" & SeriesColumnLetter(i) & "65536")
> DataNumber = Application.WorksheetFunction.CountA(SeriesRange(i))
> DataSelection(i) = SeriesColumnLetter(i) & "7:" &
> SeriesColumnLetter(i) & (DataNumber + 6)
>
> Set DataSource(i) = Worksheets("Data").Range(DataSelection(i))
>
> Else
>
> MsgBox ("No column letter!")
>
> End If
>
> Next
>
> SelectSeries = DataSource()
>
> End Function
>
> Four SeriesColumnLetter variables are input into the function.Usually
> these values are a letter, but sometimes they are blank. If they are
> blank, I would like them to be excluded from the returning array i.e.
> if 1 out of the 4 is blank, then the returning array would contain 3
> values.
>
> At the line:
>
> Set DataSource(i) = Worksheets("Data").Range(DataSelection(i))
>
> I get the error: 'Subscript out of range'. I guess this is because the
> array has been declared dynamically. How can I get around this and
> return a dynamic array??
>
> Thanks and regards,
>
> Tom


 
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
Assigning named range to a 2-dimensional array =?Utf-8?B?Qm9i?= Microsoft Excel Programming 5 4th Sep 2007 11:28 AM
Assigning an array to range object? =?Utf-8?B?U21pdGNo?= Microsoft Excel Programming 2 16th Feb 2007 03:10 PM
assigning array back to range =?Utf-8?B?SmFuZQ==?= Microsoft Excel Programming 1 11th Nov 2004 07:45 AM
Assigning Range to Array =?Utf-8?B?U3RyYXR1c2Vy?= Microsoft Excel Programming 4 30th Oct 2004 06:23 AM
Assigning range to array not working Marston Microsoft Excel Programming 2 12th Aug 2004 11:38 PM


Features
 

Advertising
 

Newsgroups
 


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