Excel VBA Array Function...What's wrong?
Good answers, all. FYI the "Test =" line was just a symptom of the fact that
I renamed my Function from "Test" before I posted it.
Range.Value returns an array, that's handy :)
Anyway, thanks, I'll try these mods in the morning.
"Dave Peterson" wrote:
I think that you missed the most important line in your code. You have to tell
it what the function returns:
Range2Array = Result
instead of:
Test = Result
Option Explicit
Function Range2Array(Source As Range) As Variant
Dim rowCount As Long
Dim colCount As Integer
Dim Result() As Variant
Dim i As Long
Dim j As Long
'single area check
Set Source = Source.Areas(1)
rowCount = Source.Rows.Count
colCount = Source.Columns.Count
' Ensure target is a range equal in size to source
If TypeOf Application.Caller Is Range Then
If Application.Caller.Columns.Count < colCount _
And Application.Caller.Rows.Count < rowCount Then
Range2Array = CVErr(xlErrRef)
Exit Function
End If
End If
' Load result array
ReDim Result(1 To rowCount, 1 To colCount)
'Result(1, 1) = "Test"
For i = 1 To rowCount
For j = 1 To colCount
' At this point Source(i,j).Value is not Empty
Result(i, j) = Source(i, j).Value
' At this point the value of Result is Empty.
Next j
Next i
' Return result array
Range2Array = Result
End Function
I also declared your variables as long instead of integers and variants.
dim i,j as long
declares i as a variant and j as a long.
======
And if you really just wanted to pick up the values in a single area range, you
could use:
dim myArr as variant 'no ()'s here
myarr = source.value
or
myarr = source.areas(1).value
This results in a two dimensional array (x rows by y columns). It's even 2
dimensional if you passed it a single column (x rows by 1 column).
Kind of like:
dim myArr(1 to 99, 1 to 1)
Spatters71 wrote:
I am just trying to create an array function that returns an array containing
the values in a source Range. It shouldn't be that difficult, right? My
source is below. I am running into the problem on this line: "Result(i, j) =
Source(i, j).Value". The Result element is Empty after the assignment, even
though Source(i,j).Value is not Empty. Help!? If you know why this assignment
isn't working or if you know a better way to do this, please let me know.
Function Range2Array(Source As Range) As Variant
Dim rowCount, colCount As Integer
rowCount = Source.Rows.count
colCount = Source.Columns.count
' Ensure target is a range equal in size to source
If Not (TypeOf Application.Caller Is Range _
And Application.Caller.Columns.count = colCount _
And Application.Caller.Rows.count = rowCount _
) Then
Exit Function
End If
' Load result array
Dim Result() As Variant
ReDim Result(1 To rowCount, 1 To colCount) As Variant
Dim i, j As Integer
Result(1, 1) = "Test"
For i = 1 To rowCount
For j = 1 To colCount
' At this point Source(i,j).Value is not Empty
Result(i, j) = Source(i, j).Value
' At this point the value of Result is Empty.
Next j
Next i
' Return result array
Test = Result
End Function
--
Dave Peterson
|