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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
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>
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,598
Members
449,089
Latest member
Motoracer88

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
Back
Top