Hi,

When it errors, click on debug and move the cursor over n and j and find the value. If the current value is greater than 50000 and 100 of n and j respectively, then replace the statement

redim k(1 to 50000,1 to 100)

with

redim k(1 to 100000,1 to 200)

or whatever the maximum possible rows or columns of output data.