Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dim xl As New Excel.Application
Dim xlBook As Excel.Workbook = xl.Workbooks.Open(myExcelFileName)
Dim xlSheet As Excel.Worksheet = xlBook.Sheets("Sheet1")


So I have the above code that open excel file to perform some operation later, The problem is sme sheets contain space in there names for example "sheetx "

And when trying to read the sheet Dim xlSheet As Excel.Worksheet = xlBook.Sheets("Sheet1")

Its catching an error HRESULT 0x8002000B
Posted

1 solution

That error means "Bad index" (see this link[^])

If you don't know what the name of the sheet is, just that it will always be the first sheet you could do:
xlBook.Sheets(1)

another option is if the user inputs the name of the sheet you could iterate around each sheet comparing a trimmed version of the name. The .Trim() function can be used to remove all leading and trailing whitespace characters from a string. This wouldn't be very efficient however as it does need to go around each sheet.

A third option, would be to get a list of sheet names and ask the user which one they wish to access.
 
Share this answer
 
Comments
[no name] 13-Feb-14 10:00am    
Can't use index since its not fixed I need to get worksheet by name.
[no name] 13-Feb-14 10:01am    
Where I can trim? the space is from the excel side???
Pheonyx 13-Feb-14 10:09am    
The space would be in the sheet name by who ever created the excel file.. so if you would probably need to use some sort of iteration.. loop through each sheet... read the sheet name, trim it and see if it matches the one you are looking for.

Sudo code:

foreach(var s in xlbook.sheets)
{
if(s.sheetname.trim() == "sheet1"
{
sheet = s
break out of loop
}
}
[no name] 13-Feb-14 10:18am    
Nice piece of code. if you like my question upvote it and post this as answer
[no name] 13-Feb-14 10:02am    
If you like my question upvote it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900