ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Assign Range passed to fuction to an array (https://www.excelbanter.com/excel-programming/424995-assign-range-passed-fuction-array.html)

Michael[_4_]

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.

Michael[_4_]

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

Mike H

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.


Per Jessen

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.



Mike H

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



All times are GMT +1. The time now is 08:10 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com