+ Reply to Thread
Results 1 to 4 of 4

what is right or wrong-Using A Macro To Add Event Procedure To A CommandButton

  1. #1
    Donna
    Guest

    what is right or wrong-Using A Macro To Add Event Procedure To A CommandButton

    Folkes,
    I know there are plenty of question/discussions relating to this but I
    am not understanding.
    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.

    Can any of you wizards out there help me out.
    Cheers

  2. #2
    anonymousA
    Guest

    re: what is right or wrong-Using A Macro To Add Event Procedure To A CommandButton

    Hi,

    Doing so with one of these, Donna you should fix your problem

    Private Sub CommandButton1_Click()

    Dim StartLine As Long
    With
    ActiveWorkbook.VBProject.VBComponents(ActiveSheet.CodeName).CodeModule
    StartLine = .CreateEventProc("Click", "CommandButton2")

    .InsertLines .ProcBodyLine("CommandButton2_Click", vbext_pk_Proc) +
    1, "d = Range(""E1"")" & Chr(13) & "Call Common "
    End With
    End Sub

    OR

    Private Sub CommandButton1_Click()

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

    So long,

    Donna a écrit :
    > Folkes,
    > I know there are plenty of question/discussions relating to this but I
    > am not understanding.
    > 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.
    >
    > Can any of you wizards out there help me out.
    > Cheers


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

    Red face Thanks...I'll give them a go.

    Thanks for the help, I will give them a go and hopefully you have put me out of my misery!....Many Thanks.

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

    Angry I can't get them to work-Using Macro To Write A Macro

    Anonymous Guest.....
    I've tried both of your suggestions and unfortunately I can not get them to work.
    The first method seems to want an '=' where the '+' is after..vbext_pk_Proc) and the second method comes up with a Memory Access Error at 000xxxetc when it tries to run the .Insert Line. Does anybody know why?
    Can anybody reccommend a good site with a good basic understanding of how to use macros to write macros....I'm not having much luck in finding one and I am not getting very far very quickly in understanding the code that 'Anonymous Guest' very kindly suggested.

    Thanks for anybodies help.

+ 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