Passing Arrays

Alan Nicoll

Board Regular
Joined
Feb 3, 2004
Messages
71
Annoying little problem. I want to pass an array to a function and have the function return the array, modified. But this fails:
Code:
TestArray = Condense(TestArray)
I get an error message that I can't assign an array this way.

I can create a Variant variable to receive the result, i.e., this works:
Code:
Dim TestArray(10,10) as String
Dim NewArray as Variant

NewArray = Condense(TestArray)
But if I want to continue calling my data "TestArray" I have to assign it to NewArray, element by element.

Do I just have to bite the bullet and make TestArray Public?

Seeking guidance. :pray:
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Greg Truby

MrExcel MVP
Joined
Jun 19, 2002
Messages
10,022
Maybe I'm missing something? You cannot pass arrays by value (byVal), you must pass them by reference (byRef). So your code should alter the values inside the array anyway.

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> test()
    <SPAN style="color:#00007F">Dim</SPAN> s(1 <SPAN style="color:#00007F">To</SPAN> 3) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    
    s(1) = "a"
    s(2) = "b"
    s(3) = "c"
    
    Debug.Print s(1), s(2), s(3)
    MakeUpper s
    Debug.Print s(1), s(2), s(3)
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> MakeUpper(s() <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>)
    <SPAN style="color:#00007F">For</SPAN> i = <SPAN style="color:#00007F">LBound</SPAN>(s) <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(s)
        s(i) = UCase(s(i))
    <SPAN style="color:#00007F">Next</SPAN> i
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,332
Messages
5,769,470
Members
425,552
Latest member
learnerrr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top