VB 工作表上命名表的 Com 错误
VB Com Error for Naming Sheets on a Worksheet
我不断收到一条错误消息
"An unhandled exception of type
'System.Runtime.InteropServices.COMException' occurred in
Microsoft.VisualBasic.dll"
Additional information: Exception from HRESULT: 0x800A03EC"
在我试图从 Workbook reportApp 更改工作表名称的那一行。在我的 timeWorkbook 上,单元格 A1 中有标题,然后是单元格 D1,依此类推。
我希望它循环直到没有更多值,但我无法更改名称。如果输入 reportApp.Sheets(s).Name = "Name this sheet",我可以更改该工作簿中工作表的名称,但我不想那样做。我想知道我的类型或代码是否有任何问题可以解决这个问题?
Private Sub generateReportButton_Click(sender As Object, e As EventArgs) Handles generateReportButton.Click
Dim timeApp As Excel.Application = New Excel.Application
Dim timeClockPath As String = "C:\Users\njryn_000\Desktop\Project ACC\Clock-In Excel\TimeClock.xlsx"
Dim timeWorkbook As Excel.Workbook = timeApp.Workbooks.Open(timeClockPath, ReadOnly:=False, IgnoreReadOnlyRecommended:=True, Editable:=True)
Dim timeWorksheet As Excel.Worksheet = timeWorkbook.Worksheets("TA")
Dim reportApp As Excel.Application = New Excel.Application
Dim reportPath As String = "C:\Users\njryn_000\Desktop\Project ACC\Report\Blank Timecard Report9.xlsx"
Dim reportWorkbook As Excel.Workbook = reportApp.Workbooks.Open(reportPath, ReadOnly:=False, IgnoreReadOnlyRecommended:=True, Editable:=True)
Dim reportWorksheet As Excel.Worksheet = reportWorkbook.Worksheets("Sheet" & 1)
Dim s As Integer
Dim i As Integer
Dim f As Integer
Dim taName As String
Dim taID As String
i = 0
f = 0
s = 1
With timeWorksheet.Range("A1")
Do
i += 3
s += 1
reportApp.Sheets(s).Name = timeWorksheet.Range("A1").Offset(0, i).Value
Loop Until IsNothing(timeWorksheet.Range("A1").Offset(0, 0).Offset(0, i).Value)
您已经解决了您的问题,但您可能无法添加答案,所以这里有一些反馈。
当您使用 With
块时,您可以在其后用一个点 ('.') 引用您在该块顶部引用的任何内容。它是一种语法,可以减少一遍又一遍地写同样的东西。在下面的代码片段中,我删除了对 timeWorksheet.Range("A1")
的所有引用并添加了一个前导点。
With timeWorksheet.Range("A1")
Do
i += 3
s += 1
' Since you are using a With block this statement is simplified.
reportApp.Sheets(s).Name = .Offset(0, i).Value
' I removed the .Offset(0, 0) as it is redundant.
' If you have it in to solve a bug you can put it back.
Loop Until IsNothing(.Offset(0, i).Value)
' More code here...
End With
您还意识到可以使用 Val()
function to fix your code. Reading the documentation,它解释说此函数将接受一个字符串并开始从中读取一个数字,忽略空格。一旦它到达一个非数字、非空白字符,它就会停止,并且 returns 这个数字会忽略字符串中的任何其他内容。
这似乎并没有真正解决您的问题,只是解决了这个问题。我会查看您正在循环的单元格中存在哪些其他字符并明确处理它们。否则你可能会得到奇怪的结果。
我不断收到一条错误消息
"An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.VisualBasic.dll"
Additional information: Exception from HRESULT: 0x800A03EC"
在我试图从 Workbook reportApp 更改工作表名称的那一行。在我的 timeWorkbook 上,单元格 A1 中有标题,然后是单元格 D1,依此类推。
我希望它循环直到没有更多值,但我无法更改名称。如果输入 reportApp.Sheets(s).Name = "Name this sheet",我可以更改该工作簿中工作表的名称,但我不想那样做。我想知道我的类型或代码是否有任何问题可以解决这个问题?
Private Sub generateReportButton_Click(sender As Object, e As EventArgs) Handles generateReportButton.Click
Dim timeApp As Excel.Application = New Excel.Application
Dim timeClockPath As String = "C:\Users\njryn_000\Desktop\Project ACC\Clock-In Excel\TimeClock.xlsx"
Dim timeWorkbook As Excel.Workbook = timeApp.Workbooks.Open(timeClockPath, ReadOnly:=False, IgnoreReadOnlyRecommended:=True, Editable:=True)
Dim timeWorksheet As Excel.Worksheet = timeWorkbook.Worksheets("TA")
Dim reportApp As Excel.Application = New Excel.Application
Dim reportPath As String = "C:\Users\njryn_000\Desktop\Project ACC\Report\Blank Timecard Report9.xlsx"
Dim reportWorkbook As Excel.Workbook = reportApp.Workbooks.Open(reportPath, ReadOnly:=False, IgnoreReadOnlyRecommended:=True, Editable:=True)
Dim reportWorksheet As Excel.Worksheet = reportWorkbook.Worksheets("Sheet" & 1)
Dim s As Integer
Dim i As Integer
Dim f As Integer
Dim taName As String
Dim taID As String
i = 0
f = 0
s = 1
With timeWorksheet.Range("A1")
Do
i += 3
s += 1
reportApp.Sheets(s).Name = timeWorksheet.Range("A1").Offset(0, i).Value
Loop Until IsNothing(timeWorksheet.Range("A1").Offset(0, 0).Offset(0, i).Value)
您已经解决了您的问题,但您可能无法添加答案,所以这里有一些反馈。
当您使用 With
块时,您可以在其后用一个点 ('.') 引用您在该块顶部引用的任何内容。它是一种语法,可以减少一遍又一遍地写同样的东西。在下面的代码片段中,我删除了对 timeWorksheet.Range("A1")
的所有引用并添加了一个前导点。
With timeWorksheet.Range("A1")
Do
i += 3
s += 1
' Since you are using a With block this statement is simplified.
reportApp.Sheets(s).Name = .Offset(0, i).Value
' I removed the .Offset(0, 0) as it is redundant.
' If you have it in to solve a bug you can put it back.
Loop Until IsNothing(.Offset(0, i).Value)
' More code here...
End With
您还意识到可以使用 Val()
function to fix your code. Reading the documentation,它解释说此函数将接受一个字符串并开始从中读取一个数字,忽略空格。一旦它到达一个非数字、非空白字符,它就会停止,并且 returns 这个数字会忽略字符串中的任何其他内容。
这似乎并没有真正解决您的问题,只是解决了这个问题。我会查看您正在循环的单元格中存在哪些其他字符并明确处理它们。否则你可能会得到奇怪的结果。