VBA versions of Array.add and the += operator

vbanative

New Member
Joined
Mar 30, 2013
Messages
8
I am trying to write a couple lines of code in VBA, here is what the VB.Net version of the code looks like.

<code class="keyword"></code>
<code class="plain"></code>

<tbody>
</tbody>

<code class="spaces"> ThisVariable</code><code class="plain"> <<= 8</code>

<tbody>
</tbody>

<code class="spaces"></code><code class="plain"> </code>

<tbody>
</tbody>

<code class="spaces"> </code><code class="plain">Variable.Add(ThisVariable)</code>

<tbody>
</tbody>

How do I write these lines of code in VBA? VBA does not have the "<<=" operator and does not have the .Add property of an array. Any help would be greatly appreciated.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
anybody have any suggestions for how to add an element to the end of array? In VB.net it would simply arrayname.add
Assuming it is a one-dimensional array named MyArray...

ReDim Preserve MyArray(LBound(MyArray) To UBound(MyArray) + 1)
MyArray(UBound(MyArray)) = NewValue

Note though that ReDim Preserve is not a fast operation (VB reallocates a new chunk of memory for the array each time it is executed), so if you are doing this repeatedly in a loop, it is better to ReDim Preserve in large chunks rather than one at a time, check if you have filled to the last array element and then ReDim Preserve a new large chunk. Keep track of elements with an index variable and at the end of the loop, do one last ReDim Preserve using the last used index value for the arrays final upper bound setting.
 
Upvote 0
Note though that ReDim Preserve is not a fast operation (VB reallocates a new chunk of memory for the array each time it is executed), so if you are doing this repeatedly in a loop, it is better to ReDim Preserve in large chunks rather than one at a time, check if you have filled to the last array element and then ReDim Preserve a new large chunk. Keep track of elements with an index variable and at the end of the loop, do one last ReDim Preserve using the last used index value for the arrays final upper bound setting.
Here is a link to code I posted online once which shows the concept...

http://www.mrexcel.com/forum/excel-questions/655300-better-array-collection-objects.html#post3248913
 
Upvote 0

Forum statistics

Threads
1,203,524
Messages
6,055,904
Members
444,832
Latest member
bgunnett8

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