0

As I'm now aware, array in Excel have two kinds of representation :

{1,2,3,4} also known as "one-dimensional horizontal array"

and

{1;2;3;4} also known as "one-dimensional vertical array"

I created a VBA function that return an array but when I use it in a function that need arrays (SUMPRODUCT), Excel display it with commas. So it always interpret it as an horizontal array. It's fine if I want to have the product with another horizontal array but it doesn't work when I try with a vertical array.

VBA :

Function MyRange()
    Dim output(2)
    output(0) = 1
    output(1) = 1
    output(2) = 1
    MyRange = output
End Function

Excel

=SUMPRODUCT(MyRange();{1,1,1}) works
=SUMPRODUCT(MyRange();{1;1;1}) is not working

My question ?

How I can have Excel to show {1;1;1} instead of {1,1,1} in the 1st array ?

enter image description here

4
  • 3
    =SUMPRODUCT(TRANSPOSE(MyRange());{1;1;1})? Commented Jan 4, 2017 at 21:26
  • @ScottCraner If you want to create an answer. I will accept it. Many thanks ! Commented Jan 4, 2017 at 21:32
  • 1
    or maybe INDEX({1;1;1},0,1) or MyRange = Application.Transpose(output) in VBA Commented Jan 4, 2017 at 21:57
  • @Slai Nice catch with TRANSPOSE directly in the VBA. It works ! Commented Jan 4, 2017 at 22:57

2 Answers 2

2

Use Tranpose to switch it:

=SUMPRODUCT(MyRange();TRANSPOSE({1;1;1}))
Sign up to request clarification or add additional context in comments.

4 Comments

Really weird. It looks perfectly in Function Wizard but Excel still return #VALUE! when I click OK !
I put the TRANSPOSE on the other part =SUMPRODUCT(MyRange();TRANSPOSE({1;1;1})) and it's working now ! Many thanks !
Finally, and unfortunately for me, TRANSPOSE seems to work only on "hardcoded" array. If I use another EXCEL function instead like TRANSPOSE(--(WEEKDAY(D3:D1257,3)<5)) or --(TRANSPOSE(WEEKDAY(D3:D1257,3))<5), the wizard show it correctly but not in the cell :(
I follow @Slai suggestion and putting TRANSPOSE directly in the VBA do the trick :) TRANSPOSE was the right direction. Thanks !
1

Another a bit faster option is to use a "vertical" array (not tested):

Function MyRange()
    Dim output(1 To 3, 1 To 1)
    output(1, 1) = 1
    output(2, 1) = 2
    output(3, 1) = 3
    MyRange = output
End Function

or a bit slower (also not tested):

Function MyRange()
    MyRange = [{1;2;1}]
    MyRange(3, 1) = 3
End Function

Comments

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.