包含空格的工作表出错 [英] An error with worksheet that contain space
问题描述
Dim xl As New Excel.Application
Dim xlBook As Excel.Workbook = xl.Workbooks.Open(myExcelFileName)
Dim xlSheet As Excel.Worksheet = xlBook.Sheets("Sheet1")
所以我有上面的代码打开excel文件以便以后执行某些操作,问题是表格包含空格,例如sheetx
当试图读取工作表时Dim xlSheet As Excel.Worksheet = xlBook.Sheets(Sheet1)
它捕获错误HRESULT 0x8002000B
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
推荐答案
该错误意味着错误索引(查看此链接 [ ^ ] )
如果你不知道的名字是什么的表是,它只是你可以做的第一张表:
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)
另一种选择是,如果用户输入可以在每张纸上迭代的纸张名称,比较名称的修剪版本。 .Trim()函数可用于从字符串中删除所有前导和尾随空白字符。这不会非常有效,因为它确实需要绕过每张纸。
第三种选择,就是得到一个工作表名称列表并询问用户他们想要访问哪一个。
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.
这篇关于包含空格的工作表出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!