Solved

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

Posted on 2010-11-12
4
499 Views
Last Modified: 2012-05-10
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.

0
Comment
Question by:sjgrey
  • 2
4 Comments
 
LVL 50

Expert Comment

by:Dave Brett
ID: 34126334
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
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 500 total points
ID: 34126376
Maybe I am not understanding this but why can't you use ReDim?

You can always increase the size and keep your existing data using preserve

Redim preserve ....


then reading this

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.

could you not do something like this?

    Dim x() As String
   
    ReDim x(Len(sStr1), Len(sStr2))
0
 
LVL 1

Author Closing Comment

by:sjgrey
ID: 34126388
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
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 34126410
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
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question