0

What my code is trying to accomplish is copying all the cells from one worksheet into another. It is the simple block

// find the index of the first empty row and column in the dumped file
int m = 1; while ( !wb.Worksheet(3).Cell(m,1).IsEmpty() ) ++m; 
int n = 1; while ( !wb.Worksheet(3).Cell(1,n).IsEmpty() ) ++n;
// copy from dumped file into raw data file
for (int i = 1; i < m; ++i )
{
    for (int j = 1; i < n; ++j)
    {
        wb.Worksheet(1).Cell(i,j).Value = wb.Worksheet(3).Cell(i,j).Value;
    }
}

and somehow this is throwing the error

Column number must be between 1 and 16384

Any idea why that could be? I don't see any infinite loops or anything like that.

Subpar Web Dev
  • 3,210
  • 7
  • 21
  • 35

2 Answers2

3

There are two things going on with this line of code.

for (int j = 1; i < n; ++j)
  1. There is no limit on j; it will just just keep increasing while i < n. It should probably be:
          for (int j = 1; j < n; ++j)
  2. This is most likely unrelated but ++j increments before j is used. Use j++ to increment after j is used. Your code could be written as
          for (int j = 2; j < n; j++)
          I'm not sure if you want the first iteration to start with j as 1 or 2.
0

It looks like you're looping until you find an empty row or column. If it doesn't find one (seems unlikely) then it's definitely going to go past the boundaries.

What are you using? Is it interop? You could check for Worksheet.UsedRange.Rows.Count and Worksheet.UsedRange.Columns.Count. Depending on what you're trying to do you could possibly just copy and paste Worksheet.UsedRange instead of copying the values one cell at a time.

Scott Hannen
  • 27,588
  • 3
  • 45
  • 62