2D array declaration problems

cs1ctp

Board Regular
Joined
Oct 7, 2005
Messages
122
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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
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)
 
Upvote 0
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.

Thanks again for your help
 
Upvote 0

Forum statistics

Threads
1,225,846
Messages
6,187,363
Members
453,420
Latest member
ESCH1021

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