Solved

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

Posted on 2010-11-12
4
495 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now