cypress1976
Member
- Joined
- Aug 18, 2009
- Messages
- 7
- Programming Experience
- 3-5
I have a program that is reading an Excel (xlsx) file and I am trying to take the data found in one of the cells and split it based upon the '@' symbol inside that cell.
Here's an example:
value in cell is "XXX @ YYY"
I declare 3 strings (strOriginal, strA, strH)
strOriginal is the value from the cell ("XXX @ YYY")
then my code is supposed to split up the values in the Original string based upon that '@' symbol.
So when my code is done, strA should = 'XXX' and strH should = 'YYY'.
Here is my code
While debugging my code, I know that the strOriginal is getting the proper "XXX @ YYY" value and I also know that y is getting the index position of the '@' symbol - in this instance, it would be '4'. And strA gets the proper modified value of "XXX".
The problem occurs in the last line of the above code
For some unknown reason, the y integer (in this case, it is '4') changes to a random number (it has changed to 44, 61, 124) and causes the error "Error with Excel file: Index and length must refer to a location within the string. Parameter name: length" to occur.
I understand why the error occurs (my y integer is not staying at 4)...I just don't understand why that y integer is changing numbers when it should remain as the indexof the '@' symbol. I'm looking for any suggestions on how to pull out the data after the space after the '@' symbol (the 'YYY' portion of 'XXX @ YYY'). Also sometimes the 'YYY' is only 'YY' as well.
Hopefully I have provided enough information. Thanks.
Here's an example:
value in cell is "XXX @ YYY"
I declare 3 strings (strOriginal, strA, strH)
strOriginal is the value from the cell ("XXX @ YYY")
then my code is supposed to split up the values in the Original string based upon that '@' symbol.
So when my code is done, strA should = 'XXX' and strH should = 'YYY'.
Here is my code
VB.NET:
strOriginal = dtbExcelData.Rows(0).Item(0).ToString
y = strOriginal.IndexOf("@")
strA = strOriginal.Substring(0, y - 1)
strH = strOriginal.Substring(y + 1, strOriginal.Length - 1)
While debugging my code, I know that the strOriginal is getting the proper "XXX @ YYY" value and I also know that y is getting the index position of the '@' symbol - in this instance, it would be '4'. And strA gets the proper modified value of "XXX".
The problem occurs in the last line of the above code
VB.NET:
strH = strOriginal.Substring(y + 1, strOriginal.Length - 1)
For some unknown reason, the y integer (in this case, it is '4') changes to a random number (it has changed to 44, 61, 124) and causes the error "Error with Excel file: Index and length must refer to a location within the string. Parameter name: length" to occur.
I understand why the error occurs (my y integer is not staying at 4)...I just don't understand why that y integer is changing numbers when it should remain as the indexof the '@' symbol. I'm looking for any suggestions on how to pull out the data after the space after the '@' symbol (the 'YYY' portion of 'XXX @ YYY'). Also sometimes the 'YYY' is only 'YY' as well.
Hopefully I have provided enough information. Thanks.