Link to home
Start Free TrialLog in
Avatar of sjgrey
sjgreyFlag for Australia

asked on

VBA two dimensional array within function - size not known in advance

I am writing a user defined function that will process two strings.  I need a two dimesional array with one dimension equalling the length of the first string and the other equalling the length of the second string.  The string lengths are not known in advance.

As ReDim is confined to the last dimension of an array, I don't think I can use it.

Is there a simple way to set up an array precisely sized for the function on each call or do I need to declare a fixed array bigger than the largest strings I expect to handle?

I've made use of dictionaries for other aspects of the module but this part really needs to be set up as a 2D array akin to a mathematical matrix.  The contents of the array are type Single variables and the function returns a type Single value.

This seesm to be a matter of widespread interest but I haven't found a way around it in an extensive search.

Avatar of Dave
Dave
Flag of Australia image

Can you pls provide some further detail, worked samples?

I think you will need to overspecify the size, you can trim the last dimension when the final length is known.

Cheers

Dave
ASKER CERTIFIED SOLUTION
Avatar of rockiroads
rockiroads
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sjgrey

ASKER

I had thought I needed to initially dimension the array with two dimensions then resize it when I had the string lengths, which fails, but dimensioning it with nothing in the brackets then using ReDim with two dimensions seems to work.

Thanks very much
ok. I had thought you must of tried hence my question about redim. But no worries. This is what you just need to remember. If unknown size until afterwards, just define an empty array (like in the example) and your all sorted