# 2D array declaration problems

#### cs1ctp

##### Board Regular
Hi,

I have this code to read a text file and populate an array and a 2D array with the values from the file. However it gets stuck in an endless loop. I think it may have something to do with my 2D array declaration, but i'm not sure. It worked perfectly when I had three single arrays instead of the 2D array.

Code:
``````Dim ArrayContract() As Variant      'declare as a variable length array
Dim ArrayPosition() As Variant        '2D array

Open "U:/contracts.txt" For Input As #1
k = 0
i = 0
j = 0

On Error Resume Next
Do While Not EOF(1)

ReDim Preserve ArrayContract(k + 1) As Variant   'resize the array to add another place
ReDim Preserve ArrayPosition(i + 1, j + 1) As Variant     'resize the array to add another place

Input #1, ArrayContract(k), ArrayPosition(i, j)

k = k + 1
i = i + 1
j = j + 1
Loop
Close #1``````

Many Thanks,

Chris

### Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},\$Z\$1:\$Z\$99,\$Y\$1:\$Y\$99),2,False) to lookup Y values to left of Z values.
I don't know about the endless loop, but from Help:

If you use the Preserve keyword, you can resize only the last array dimension and you can't change the number of dimensions at all. For example, if your array has only one dimension, you can resize that dimension because it is the last and only dimension. However, if your array has two or more dimensions, you can change the size of only the last dimension and still preserve the contents of the array. The following example shows how you can increase the size of the last dimension of a dynamic array without erasing any existing data contained in the array.

ReDim X(10, 10, 10)
. . .
ReDim Preserve X(10, 10, 15)

Thanks for your help, Preserve is defintiely the problem, however if I remove Preserve from the 2D array, the first array gets populated with all the values from the file instead of the first value going into the first array and the second two going into the 2D array.

As a result I will reconsider the way I am implementing this and use three arrays as I was doing before.

Replies
5
Views
55
Replies
0
Views
55
Replies
13
Views
875
Replies
7
Views
559
Replies
5
Views
444

1,202,964
Messages
6,052,829
Members
444,602
Latest member
Cookaa

### 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.

### Which adblocker are you using?

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

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