Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default Assign Range passed to fuction to an array

I am looking for an example of how to pass a function a range and
assign it to an array so I can access the values with an index number.
For some reason I cant get this to work.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default Assign Range passed to fuction to an array

On Mar 3, 2:00*pm, Michael wrote:
I am looking for an example of how to pass a function a range and
assign it to an array so I can access the values with an index number.
For some reason I cant get this to work.


Forgot to indclude example

Function testfunc2(rng As Range)
Dim r As Variant
Dim arr As Variant
Dim x As Integer

'arr = rng

'assign range values to array
For Each r In rng
arr(x) = r.Value
x = x + 1
Next

'list array values
For x = LBound(arr) To UBound(arr)
Debug.Print arr(x).Value
Next x

testfunc2 = "test"
End Function
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Assign Range passed to fuction to an array

Michael,

This may do what you want. take this simple function below. Called with a
range object you can refer to each cell of the range using .item.

I've included 2 message boxes in the function to demonstrate the method but
in practice these would be replaced with your code.

=myvalue(A1:A10)

Public Function MyValue(rng As Range) As Double
Dim x As Long
With rng
For x = 1 To .Count
MsgBox .Item(x).Address
MsgBox .Item(x).Value
Next x
End With
End Function


Mike

"Michael" wrote:

I am looking for an example of how to pass a function a range and
assign it to an array so I can access the values with an index number.
For some reason I cant get this to work.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,533
Default Assign Range passed to fuction to an array

Hi

Try this:

MyArray = Array(Range("A1:A10").Value)

Regards,
Per

"Michael" skrev i meddelelsen
...
I am looking for an example of how to pass a function a range and
assign it to an array so I can access the values with an index number.
For some reason I cant get this to work.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Assign Range passed to fuction to an array

Michael,

Your function simplified

=testfunc2(A1:A10)

Function testfunc2(rng As Range) As Double
Dim r As Variant
Dim arr As Variant
Dim x As Integer

For x = 1 To rng.Count
Debug.Print rng.Item(x).Value
Debug.Print rng.Item(x).Address
Next x

End Function


Mike

"Michael" wrote:

On Mar 3, 2:00 pm, Michael wrote:
I am looking for an example of how to pass a function a range and
assign it to an array so I can access the values with an index number.
For some reason I cant get this to work.


Forgot to indclude example

Function testfunc2(rng As Range)
Dim r As Variant
Dim arr As Variant
Dim x As Integer

'arr = rng

'assign range values to array
For Each r In rng
arr(x) = r.Value
x = x + 1
Next

'list array values
For x = LBound(arr) To UBound(arr)
Debug.Print arr(x).Value
Next x

testfunc2 = "test"
End Function

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
Assign a 2D array of constant values to a range dodgo Excel Programming 4 October 19th 06 04:46 AM
Excel 2003. Assign array to range Jorge Vinuales Excel Programming 2 May 2nd 06 06:06 PM
Translate range name passed as string to a custom function to range addresses! agarwaldvk[_25_] Excel Programming 3 September 7th 04 12:47 PM
Error Converting Passed Range into Array in VBA for Excel Alan Beban[_2_] Excel Programming 0 September 1st 04 04:56 PM
Error Converting Passed Range into Array in VBA for Excel Dave Peterson[_3_] Excel Programming 0 September 1st 04 01:17 AM


All times are GMT +1. The time now is 03:02 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"