0

In VBA is it possible to loop through, for example, every column of a 2 dimensional array and perform and operation on each column? I would like to pass each column or row of an array sequentially to a function.

Thanks.

6
  • I would like to pass each column or row of an array sequentially to a function. - you should create new 1D array, populate it with values of column/row of your 2D array, and pass it in function Commented Mar 31, 2014 at 14:40
  • 1
    @simpLEMAn mind sharing how you would do it? Commented Mar 31, 2014 at 14:40
  • any better solutions? Commented Mar 31, 2014 at 14:41
  • Show us I you would do it and then we'll direct you in the right direction. Commented Mar 31, 2014 at 15:00
  • You could build a Collection of arrays(rows or colums) and then loop on each item of that collection passing them to the Function. Commented Mar 31, 2014 at 15:23

1 Answer 1

0

Normally I'd not answer this because you haven't shown any effort in trying.. But I've had my own difficulty with this (with my own question currently posted looking for a better solution) and I'm bored so:

if you have a 2D array named myArr like this:

 1,2,3,4,5
 6,7,8,9,10
 11,12,13,14,15

defined by: dim myArr(2,4) as variant

then you could loop through it with a double loop like this:

    For i = LBound(myArr) To UBound(myArr)
        For j = LBound(myArr, 2) To UBound(myArr, 2)
            temp(j) = myArr(i, j)
        Next
    Next

where temp is defined and reDim'd like this:

dim temp() as variant
redim temp(lbound(myArr,2), ubound(myArr,2)) 'note the ",2" gets the bounds of the 2nd dimention

and then pass temp to a function: FunctionName(Temp)

So put that together by sticking thue function call after the inner loop and it would loop through and pass temp to the function for each row

Sign up to request clarification or add additional context in comments.

3 Comments

I didn't "try" because this is the only way I know which is not what I am looking for. I would like to accomplish it without copying the data over and over again. I would like to pass a row or column of an array directly to a function.
Well then for future you need to include that in your details. Help Page for "On Topic" questions: "3) Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results." basically, what have you tried, and why didn't it work. this helps to eliminate unhelpful answers. I myself posted a question asking for a better way of doing this (because I don't particularly like the method I posted here either) and it took 5 mins for someone to tell me there is no other way unfortunately.
Also include details of your research efforts to solve your problem, and why the answers you found were not suitable to your situation. All of this helps us, help you, :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.