如何批量删除Excel工作簿中vba代码

如果想要用vba代码删除工作表中的vba代码,先要在excel中引入一个控件,步骤如下:

1.对于 EXCEL 2003 版本,在VBE中,工具--引用-- Microsoft Visual Basic For Application Extensibility 5.3

(不同的版本,Microsoft Visual Basic For Application Extensibility 5.3 的写法可能不同)

2.选中工具栏-->宏-->安全性...-->可靠发行商-->信任对于"Visual Basic 项目"的访问

3.接下来,如果只是想删除所有工作表中的代码,可以在vba窗口输入一下代码:

Sub Remove_some_vba_code()

    Dim activeIDE As Object 'VBProject
    Set activeIDE = ActiveWorkbook.VBProject

    Dim Element As VBComponent

    Dim LineCount As Integer
    For Each Element In activeIDE.VBComponents
        If Left(Element.Name, 5) = "Sheet" Then    
            LineCount = Element.CodeModule.CountOfLines
            Element.CodeModule.DeleteLines 1, LineCount
        End If
    Next

End Sub

如果是是要删除所有代码,就删掉中间的那个判断语句

Sub Remove_all_vba_code()

    Dim activeIDE As Object 'VBProject
    Set activeIDE = ActiveWorkbook.VBProject

    Dim Element As VBComponent

    Dim LineCount As Integer
    For Each Element In activeIDE.VBComponents
            LineCount = Element.CodeModule.CountOfLines
            Element.CodeModule.DeleteLines 1, LineCount
    Next

End Sub

 

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值