遍历当前工作簿中的工作表
Loop through worksheets in current workbook
我试图在用户单击功能区上的按钮时循环浏览活动工作簿中的所有工作表。
我遇到的唯一问题实际上是遍历工作表,出现以下错误:
Expression is of type 'Workbook', which is not a collection type.
虽然我明白这意味着什么,但我不知道如何真正实现我想要的。
Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click
Dim wks As Worksheet, objList As ListObject
For Each wks In Globals.ThisAddIn.Application.ActiveWorkbook ' ERROR LINE
For Each objList In wks.ListObjects
objList.Unlist()
Next objList
Next wks
End Sub
** 更新 **
根据评论和答案编辑代码,但它给了我一个新错误
Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets
An exception of type 'System.InvalidCastException' occurred in ConvertAllTablesToRange.dll but was not handled in user code
Additional information: Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Tools.Excel.Worksheet'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{297DC8D9-EABD-45A1-BDEF-68AB67E5C3C3}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
ActiveWorkbook.Worksheets
就是您要找的合集。
我试图在用户单击功能区上的按钮时循环浏览活动工作簿中的所有工作表。
我遇到的唯一问题实际上是遍历工作表,出现以下错误:
Expression is of type 'Workbook', which is not a collection type.
虽然我明白这意味着什么,但我不知道如何真正实现我想要的。
Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click
Dim wks As Worksheet, objList As ListObject
For Each wks In Globals.ThisAddIn.Application.ActiveWorkbook ' ERROR LINE
For Each objList In wks.ListObjects
objList.Unlist()
Next objList
Next wks
End Sub
** 更新 ** 根据评论和答案编辑代码,但它给了我一个新错误
Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets
An exception of type 'System.InvalidCastException' occurred in ConvertAllTablesToRange.dll but was not handled in user code
Additional information: Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Tools.Excel.Worksheet'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{297DC8D9-EABD-45A1-BDEF-68AB67E5C3C3}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
ActiveWorkbook.Worksheets
就是您要找的合集。