Variable Upper index of an Array

abi_learn_vba

Board Regular
Joined
Nov 6, 2009
Messages
215
Hi,

I have the below code where the Upper index of an array is not constant. The upper index of an array is decided based the value incremented for the variable "ar". So i have to use Redim every time to define the size(Upper-Index) of the array. The Values in Column "D" are e-mail ids which is stored in the array.

My problem is, when "ar = 1" the first e-mail id is stored in myarray(1), later when "ar" is increment by "1" and is equal to "2" then the second e-mail id is stored in myarray(2) but since i have redim the array with new upper index the value stored in myarray(1) is cleared and is empty. Am not sure how to go about with this scenario.

Code:
For i to Range("A" & Rows.count).End(xlup)
m = Range("A1").Value
n = Range("B1").Value
If m <> n Then
ar = ar + 1
ReDim myarray(1 To ar) As Variant
myarray(ar) = Range("D" & i).Value
End If

For j = LBound(myarray) To UBound(myarray)
mailinglist = myarray(j) & ";" & mailinglist 
Next

Any help would be appreciated. let me know if you are not clear with my question.

Thanks
-Abi
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
You must use "Redim Preserve" to prevent the existing elements of the array from being cleared.

Please see VBA help for the "preserve" keyword.

Gary
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,696
Members
452,938
Latest member
babeneker

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