Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6843897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:18:30+00:00 2026-05-27T00:18:30+00:00

I found this code online and tweaked it a bit for my need to

  • 0

I found this code online and tweaked it a bit for my need to programmatically add a command button to a spreadsheet and assign an event to it. It works well

Sub AddComm_button()
  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 Sub

Sub Modify_CommButton()
  Dim LineNum As Long 'Line number in module
  Dim SubName As String 'Event to change as text
  Dim Proc As String 'Procedure string
  Dim EndS As String 'End sub string
  Dim Ap As String 'Apostrophe
  Dim Tabs As String 'Tab
  Dim LF As String 'Line feed or carriage return

  Ap = Chr(34)
  Tabs = Chr(9)
  LF = Chr(13)
  EndS = "End Sub"
  SubName = "Private Sub abcbutton_Click()" & LF
  Proc = Tabs & "MsgBox " & Ap & "Testing " & Ap & LF
  Proc = Proc & "End Sub" & LF
  Set ModEvent = ActiveWorkbook.VBProject.VBComponents("Sheet1").CodeModule
  With ModEvent
    LineNum = .CountOfLines + 1
    .InsertLines LineNum, SubName & Proc & EndS  
  End With
End Sub

The following code appends my original program with this

Private Sub abcbutton_Click()
   MsgBox "Testing "
End Sub

and hence giving it a click event.
How to I remove the appended part after my program is done. Right now when I run my program
the second time, it already has the method abcbutton_Click() in it and it throws an error.

Thanks
Original Source : http://www.mrexcel.com/archive/VBA/5348a.html

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:18:31+00:00Added an answer on May 27, 2026 at 12:18 am

    I think what you need to do is ensure the button is only added once.

    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
    

    Also, you have a typo in your sub creation:

    Proc = Proc & "End If" & LF
    

    should be

    Proc = Proc & "End Sub" & LF
    

    Update with method to remove the code

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this code online for shuffling array elements, it works well, but I
I found this nice snippet of code online: rkApp = Registry.LocalMachine.OpenSubKey(SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run, true); Which runs
i found this code online and im trying to compile it, but gcc keeps
I found some jQuery code online for the jQuery Dialog and it works great.
I have this code that I found online that does an infinite rotating gallery,
I found this code online. I was trying to get it to write the
i found this code: protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); HwndSource hwndSource =
I found this code using Google. private int RandomNumber(int min, int max) { Random
I found this code for alphanumeric check (Letters, numbers, spaces or underscores) but I
i found this code at activestate, it takes a string and prints permutations of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.