Size limit on transferring Variant to Range in Excel 2007

nu1

New Member
Joined
May 5, 2009
Messages
6
As part of a VBA function I am outputting a large array by implicitly converting an array to a variant which I then output in an array formula. Although I am using Excel 2007, I seem to get #VALUE! errors when I dimension this with any more than 65,536 rows (also when I dimension using more than 65536 columns but only output part of the array so that it fits on the worksheet I also get these errors). I attach the code below. Is this a limit in Excel, or is there a workround?

Thanks for anyone who might be able to help

Function genarray(rows As Long, columns As Long) As Variant
Dim r As Long
Dim c As Long
ReDim x(1 To rows, 1 To columns) As Double
For r = 1 To rows
For c = 1 To columns
x(r, c) = r + c
Next c
Next r
genarray = x
End Function
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi nu1
Welcome to the board

I just used your code and had no problem.

I tested your code with:

Code:
Sub Test()
Range("A1").Resize(100000, 100).Value = genarray(100000, 100)
End Sub

Please try it.
 
Upvote 0
Thanks very much for this, pgc01 - it works brilliantly.

I'm assuming that there must be a limit on the size of the variant which can be returned when you enter the function directly into a range via an array formula - what I had tried to do was to highlight a range (say) 100000 rows * 100 cols (or even just a small range of 30 rows * 30 cols - assuming that a small subset of the array would be returned in this case) and put the formula genarray(100000,100) in and hit Ctrl-Alt-Enter, but whatever the size of the range I highlighted to receive the array formula, I got back an error unless the parameters I supplied to genarray were less than 65K.

Your solution has helped me enormously - thanks very much.
 
Upvote 0
I'm glad it helped.

You are right. If you want to use the function as a UDF, calling it directly from the worksheet in an array formula, you do get that limitation in terms of the number of rows of the array.

Remark:

... by implicitly converting an array to a variant ...

Notice that you only have to do that if you want to be compatible with the MAC. If you are using windows you can use a typed array.

For ex., in your example you are working with Longs:

Code:
Function genarrayL(rows As Long, columns As Long) As Long()
Dim r As Long
Dim c As Long
 
ReDim x(1 To rows, 1 To columns) As Long
 
For r = 1 To rows
    For c = 1 To columns
        x(r, c) = r + c
    Next c
Next r
genarrayL = x
End Function
 
Upvote 0
Thanks once again - another great tip. Why don't they tell you that in the books?! Or maybe I'm just reading the wrong books... Is there a book you'd recommend which covers this sort of stuff at this level - most of the things I do involve interfacing with DLLs and optimising array/loop type calculations and basic range/array outputs rather than exploring all the other things (charts, etc) which you can control from VBA (and which in my experience most books tend to move onto quite quickly without going through the real detail on the basics).
 
Upvote 0
I don't think there's one specific resource for what you want - the stuff on typed arrays (and lots of other VBA specific but not necessarily Excel specific) can be found in the VBA Developer's Handbook. Other useful stuff in Excel 2007 Programmer's Reference.

Otherwise, this (and other) forum(s) are your best bet.
 
Upvote 0
Thanks once again - another great tip. Why don't they tell you that in the books?!
Because they didn't figure you needed to know about it!

That's one reason books often fail to give you answers and why I'm investigating the ins and outs of on-line forums as a method of support.

Another thread going straight into the thinking pot.
 
Upvote 0

Forum statistics

Threads
1,216,101
Messages
6,128,835
Members
449,471
Latest member
lachbee

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top