delete ActiveX Button and it's procedure

luolovepi

Board Regular
Joined
Jun 9, 2011
Messages
116
Dear all,
I use the code below to delete the CommandButton1, But how could I delete its corresponding procedure "CommandButton1_Click" as well?

Code:
ActiveSheet.OLEObjects("CommandButton1").Delete

Thank you so much!
Best regards,
lolo
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hello lolo,

This may save you some time and hair pulling. Copy this code into a standard VBA module in your workbook. You can then call RemoveCode ActiveSheet.Name, "CommandButton1" from your macro.
Code:
'Written: July 04, 2011
'Author:  Leith Ross
'Summary: Removes all procedure code from a given module for a given object.

Sub RemoveCode(ByVal ModuleName As String, ByVal ObjectName)

  Dim I As Long
  Dim LineCount As Long
  Dim VBproc As String
  Dim VBproj As Object
  
    Set VBproj = ThisWorkbook.Application.VBE.ActiveVBProject
    
      With VBproj.VBComponents(ModuleName).CodeModule
        For I = .CountOfLines To 1 Step -1
          VBproc = .ProcOfLine(I, 0)
          LineCount = .ProcCountLines(VBproc, 0)
            If InStr(1, VBproc, ObjectName) Then
               .DeleteLines .ProcStartLine(VBproc, 0), LineCount
            End If
          I = I - LineCount + 1
        Next I
      End With
      
End Sub
Sincerely,
Leith Ross
 
Upvote 0
Hi thank you Leith.
Can I assign "Sheet1", which is under "Microsoft Excel Objects" instead of "Modules" in VBE, to "ModuleName"? In other words, is "Sheet1" treated as a module as well?

Best regards,
lolo
Hello lolo,

This may save you some time and hair pulling. Copy this code into a standard VBA module in your workbook. You can then call RemoveCode ActiveSheet.Name, "CommandButton1" from your macro.
Code:
'Written: July 04, 2011
'Author:  Leith Ross
'Summary: Removes all procedure code from a given module for a given object.
 
Sub RemoveCode(ByVal ModuleName As String, ByVal ObjectName)
 
  Dim I As Long
  Dim LineCount As Long
  Dim VBproc As String
  Dim VBproj As Object
 
    Set VBproj = ThisWorkbook.Application.VBE.ActiveVBProject
 
      With VBproj.VBComponents(ModuleName).CodeModule
        For I = .CountOfLines To 1 Step -1
          VBproc = .ProcOfLine(I, 0)
          LineCount = .ProcCountLines(VBproc, 0)
            If InStr(1, VBproc, ObjectName) Then
               .DeleteLines .ProcStartLine(VBproc, 0), LineCount
            End If
          I = I - LineCount + 1
        Next I
      End With
 
End Sub
Sincerely,
Leith Ross
 
Upvote 0
Hello Lolo,

Yes, you can do that because "Sheet1" is an object module of Excel. However, the macro itself must be in a standard VBA module. This is necessary for the code to be accessible by other objects in the project.

Sincerely,
Leith Ross
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,834
Members
452,947
Latest member
Gerry_F

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top