Solved

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

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

749 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