+ Reply to Thread
Results 1 to 2 of 2

Programming To The VBE -Using A Macro To Add Event Procedure To A CommandButton

  1. #1
    Registered User
    Join Date
    02-08-2005
    Posts
    4

    Cool Programming To The VBE -Using A Macro To Add Event Procedure To A CommandButton

    Am I trying to do something impossible here?
    I have a commandbutton1 on a sheet which I copy and paste using a
    macro resulting in a commandbutton2. I then want to use the macro to
    write the event code for commandbutton2. The event code I want is:-

    Private Sub CommandButton2_Click()
    d = Range("E1")
    Call Common
    End Sub

    I have a copy of www.cpearson.com - Programming To The VBE but I am
    not understanding what I have to do to write the code. I have :-

    Dim StartLine As Long
    With ActiveWorkbook.VBProject.VBComponents(ActiveSheet).CodeModule
    StartLine = .CreateEventProc("Click", "CommandButton2")
    .InsertLines StartLine, "d = Range(""E1"")" & Chr(13) & "Call
    Common"
    End With

    which I know is wrong but have no understanding of what is right or
    wrong.

    I actually get the "E1" from a FirstCol=Inputbox.....so I would like to use the contents of FirstCol rather then specifically "E1".

    Are there any wizards out there help me out because I've spent many an hour trying to understand this and am not getting anywhere.
    Cheers

  2. #2
    Forum Contributor
    Join Date
    11-16-2004
    Posts
    282

    Alternative to writing code 'on the fly'...

    Donna,

    Instead of writing code 'on the fly' in your module, since the code is already written for CommandButton2_Click, you can assign that macro to the new button you create. Insert this into your code that creates 'CommandButton2' right after the new button is created from the macro:

    ActiveSheet.Shapes(ActiveSheet.Shapes.Count).OnAction = "CommandButton2_Click"

    Hope this helps,
    theDude

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1