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.

Community
  • 1
  • 1
user3385769
  • 161
  • 6
  • 16

1 Answers1

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

user1759942
  • 1,322
  • 4
  • 14
  • 33
  • 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. – user3385769 Mar 31 '14 at 16:02
  • 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. – user1759942 Mar 31 '14 at 16:15
  • 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, :) – user1759942 Mar 31 '14 at 16:19