-1

In the following code i have errors as Index was outside the bounds of the array. Even this code done copied cells into column correctly .. does any one help me on this

string[] array = { "F3", "J3", "N3", "R3", "V3", "Z3", "AD3", "AH3", "AL3", "AP3" };
string[] arrayb={"C","G","K","O","S","W","AA","AE","AI","AM"};  


int a1count = arrayb.Length;
int b = 0;

for ( int a=0; a<= a1count; a++) 
{
 Excel.Range sourceRange = xlWorkSheet.get_Range(array[a]); 
 Excel.Range destinationRange = xlWorkSheet.UsedRange.Columns[arrayb[b]];
 sourceRange.Copy(Type.Missing);  
 destinationRange.PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteFormulas,   
 Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
 b++;
}
Snippet
  • 1,522
  • 9
  • 31
  • 66
inshaf
  • 1
  • 1
  • 1
    You could at least *try* to format your code nicely. – Hunter McMillen Mar 01 '13 at 05:56
  • possible duplicate of [Index was outside the bounds of the array - integers](http://stackoverflow.com/questions/13221309/index-was-outside-the-bounds-of-the-array-integers) – ChrisF Mar 01 '13 at 11:43

3 Answers3

1
for ( int a=0; a<= a1count; a++) 

should be

for ( int a=0; a< a1count; a++) 

Since index starts with 0. So for example for an array of 10 length index will go from 0 to 9, not 0 to 10.

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
1
for ( int a=0; a<= a1count-1; a++)
{
}
TalentTuner
  • 17,262
  • 5
  • 38
  • 63
0

it should be

for ( int a=0; a < a1count; a++) 

not

for ( int a=0; a<= a1count; a++) 

you get index out of bounds because arrays starts from 0 to length-1 try using foreach Some best Practices for coding

Snippet
  • 1,522
  • 9
  • 31
  • 66