Я думаю, вам нужно убедиться, что кнопка добавляется только один раз.
Sub AddComm_button()
Dim obj As OLEObject
Dim fFoundIt As Boolean = False
For Each obj In ActiveSheet.OLEObjects
If TypeOf obj.Object Is MSForms.CommandButton Then
If obj.Name = "abcbutton" Then
fFoundIt = True
Exit For
End If
End If
Next
If Not fFoundIt Then
Set mybutton = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1",Left:=126, Top:=96, Width:=126.75, Height:=25.5)
mybutton.Name = "abcbutton"
Call Modify_CommButton
End if
End Sub
Кроме того, у вас есть опечатка в вашем суб-создании:
Proc = Proc & "End If" & LF
должно быть
Proc = Proc & "End Sub" & LF
Обновление методом удаления кода
Sub RemoveProcedure(sProcedureName As String)
Set ModEvent = ActiveWorkbook.VBProject.VBComponents("Sheet1").CodeModule
Dim wCurrLine As Integer
Dim wFirstLine As Integer
' See if the method name exists
For wCurrLine = 1 To ModEvent.CountOfLines
Dim sCurrLine As String
sCurrLine = ModEvent.Lines(wCurrLine, 1)
If InStr(1, sCurrLine, sProcedureName, vbTextCompare) > 0 Then
wFirstLine = wCurrLine
Exit For
End If
Next
' If it does exist, remove it
If wFirstLine <> 0 Then
' Start on the line after the first line
For wCurrLine = wFirstLine + 1 To ModEvent.CountOfLines
Dim sCurrLine As String
sCurrLine = ModEvent.Lines(wCurrLine, 1)
' Found end sub
If InStr(1, sCurrLine, "End Sub", vbTextCompare) > 0 Then
' So delete the lines
ModEvent.DeleteLines wFirstLine, (wCurrLine + 1) - wFirstLine
Exit For
End If
Next
End If
End Sub