有没有办法循环遍历Excel工作表中的所有模块,并将它们全部删除?
这是我当前的代码。 它可以满足我的需要,我只是想知道是否有一种方法可以循环浏览模块,而不必键入每个模块的名称。
Dim wb As Workbook
Set wb = ActiveWorkbook
Application.ScreenUpdating = False
TempFilePath = Environ$("temp") & "\"
TempFileName = "Cable Rework" & " " & Format(Now, "mm-dd-yy") 'this names the temp file as the Pallet request followed by the current date and time
FileExtStr = ".xls": FileFormatNum = 51
wb.SaveCopyAs TempFilePath & TempFileName & FileExtStr
Workbooks.Open (TempFilePath & TempFileName & FileExtStr)
With ActiveWorkbook
.VBProject.VBComponents.Remove .VBProject.VBComponents("Module1")
.VBProject.VBComponents.Remove .VBProject.VBComponents("Module2")
.VBProject.VBComponents.Remove .VBProject.VBComponents("Module3")
.VBProject.VBComponents.Remove .VBProject.VBComponents("Module4")
.Save
.Close
End With
Application.ScreenUpdating = True
End Sub
This is my current code.
It works for what I need it for, I just wonder if there is a way to cycle through the modules rather than having to type out the name of each one.
Dim wb As Workbook
Set wb = ActiveWorkbook
Application.ScreenUpdating = False
TempFilePath = Environ$("temp") & "\"
TempFileName = "Cable Rework" & " " & Format(Now, "mm-dd-yy") 'this names the temp file as the Pallet request followed by the current date and time
FileExtStr = ".xls": FileFormatNum = 51
wb.SaveCopyAs TempFilePath & TempFileName & FileExtStr
Workbooks.Open (TempFilePath & TempFileName & FileExtStr)
With ActiveWorkbook
.VBProject.VBComponents.Remove .VBProject.VBComponents("Module1")
.VBProject.VBComponents.Remove .VBProject.VBComponents("Module2")
.VBProject.VBComponents.Remove .VBProject.VBComponents("Module3")
.VBProject.VBComponents.Remove .VBProject.VBComponents("Module4")
.Save
.Close
End With
Application.ScreenUpdating = True
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对每个使用 循环,你可以循环遍历整个
VBComponents
集合并决定哪些需要删除。该集合还包含“文档模块”每个
Worksheet
以及ThisWorkbook
都有一个,因此您可能不想删除所有内容。您可以使用 VBComponent.Type 属性检查每个组件的类型。 这里是描述不同类型的列表(转到第二个表)。标准模块为 1,类模块为 2,用户表单为 3。
一旦有了 For 循环,您只需检查
Type
是否等于 1、2 或 3,然后调用VBComponents.Remove
。注意:通常,在从集合中删除项目时循环访问集合很可能会导致循环元素出现问题。但我没有遇到此循环的任何问题,因此我没有执行通常的解决方法,将要删除的对象保存到数组中,然后再删除它们。
Using a For Each Loop, you can loop through the whole collection of
VBComponents
and decide which ones need to be deleted. The collection also contains "Document Modules"one for each
Worksheet
as well asThisWorkbook
so you may not want to delete everything.You can check the type of each component with the
VBComponent.Type
property. Here is the list describing the different types (go to the second table). Standard Modules are 1, Class Modules are 2, User Forms are 3.Once you have the For Loop, and you just need to check if
Type
is equal to 1, 2 or 3 and then callVBComponents.Remove
.Note: Normally, looping though a collection while removing items from that collection has a high chance of causing problems with the looping element. But I didn't encounter any issues with this loop, so I didn't do the usual work-around of saving the to-be-deleted objects into an array and then deleting them afterwards.