Using code to delete code

后端 未结 1 1196
小蘑菇
小蘑菇 2020-12-20 07:58

I have googled this and found multiple solutions for this but none of them have worked for me. I am inclined to believe this is because of my limited knowledge and ability w

相关标签:
1条回答
  • 2020-12-20 08:18

    Well, this code will delete the event. You save a copy of your workbook, so my idea is

    1. you do whatever you do
    2. Delete the Workbook_open Macro
    3. Save to a new workbook (that part of macro won't be)
    4. DO NOT SAVE CHANGES IN YOUR ORIGINAL WORKBOOK, so the Workbook_Open macro will be always secure.

    Anyways, test it first several times to make sure it works. Altering VBA code with VBA itself can produce weird things somethimes.

    Hope this helps.

    All credits go to OZGRIZ

    Also, make sure you check in Excel Options->Trusted Sites->Settings of Trusted Sites-> Macros Setting-> Check Trust access to Visual Basic Editor

    Sub DeleteWorkbookEventCode()
    
    ''Needs Reference Set To _
    
        '"Microsoft Visual Basic For Applications Extensibility"
    
    'Tools>References.
    
    'Also, make sure you check in Excel Options->Trusted Sites->Settings of Trusted Sites-> Macros Setting-> Check Trust access to Visual Basic Editor
    
    Dim i As Integer
    Dim MyStart, MyEnd As Integer
    
    With ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
    
    MyStart = 0
    
        For i = 1 To .CountOfLines Step 1
            If .Lines(i, 1) = "Private Sub Workbook_Open()" Then MyStart = i
    
            If .Lines(i, 1) = "End Sub" And MyStart > 0 Then
                MyEnd = i
                Exit For
            End If
        Next i
    
        .DeleteLines MyStart, (MyEnd - MyStart) + 1
    
    End With
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题