有没有办法循环遍历Excel工作表中的所有模块,并将它们全部删除?

发布于 2025-01-11 11:24:34 字数 924 浏览 3 评论 0原文

这是我当前的代码。 它可以满足我的需要,我只是想知道是否有一种方法可以循环浏览模块,而不必键入每个模块的名称。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

注定孤独终老 2025-01-18 11:24:34

对每个使用 循环,你可以循环遍历整个VBComponents集合并决定哪些需要删除。该集合还包含“文档模块”
每个 Worksheet 以及 ThisWorkbook 都有一个,因此您可能不想删除所有内容。

您可以使用 VBComponent.Type 属性检查每个组件的类型。 这里是描述不同类型的列表(转到第二个表)。标准模块为 1,类模块为 2,用户表单为 3。

一旦有了 For 循环,您只需检查 Type 是否等于 1、2 或 3,然后调用 VBComponents.Remove

Sub Example()
    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
    
    Set wb = Workbooks.Open(TempFilePath & TempFileName & FileExtStr)
    
    Dim VBComp As Variant
    For Each VBComp In wb.VBProject.VBComponents
        If VBComp.Type = 1 Or VBComp.Type = 2 Or VBComp.Type = 3 Then
            wb.VBProject.VBComponents.Remove VBComp
        End If
    Next
    wb.Save
    wb.Close
    
    Application.ScreenUpdating = True
End Sub

注意:通常,在从集合中删除项目时循环访问集合很可能会导致循环元素出现问题。但我没有遇到此循环的任何问题,因此我没有执行通常的解决方法,将要删除的对象保存到数组中,然后再删除它们。

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 as ThisWorkbook 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 call VBComponents.Remove.

Sub Example()
    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
    
    Set wb = Workbooks.Open(TempFilePath & TempFileName & FileExtStr)
    
    Dim VBComp As Variant
    For Each VBComp In wb.VBProject.VBComponents
        If VBComp.Type = 1 Or VBComp.Type = 2 Or VBComp.Type = 3 Then
            wb.VBProject.VBComponents.Remove VBComp
        End If
    Next
    wb.Save
    wb.Close
    
    Application.ScreenUpdating = True
End Sub

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文