Solved

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

Posted on 2010-11-12
4
510 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

707 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