Excel User Group
Microsoft Excel blogs, forums, files. Read, ask questions, provide answers.

Copying VBA from one sheet to another programatically

Latest post Wed, Jun 18 2008 5:34 PM by MrPogle. 3 replies.
  • Wed, Jun 18 2008 2:17 PM

    • MrPogle
    • Top 50 Contributor
    • Joined on Tue, May 27 2008
    • Posts 5
    • Points 57

    Copying VBA from one sheet to another programatically

    I have two event procedures attached to a worksheet as follows:

    Private Sub Chart_BeforeRightClick(Cancel As Boolean)
        Cancel = True
        CommandBars("myShortcutBar").ShowPopup
    End Sub
    Private Sub Chart_Activate()
        CreateShortcutMenu
    End Sub


    Some other code (attached to the right click menu made in the Chart_Activate() procedure and displayed with the right click button) creates another worksheet. I want this new worksheet, which is not created using copysheet, to have the same event procedures as above. How can I programatically write some VBA into this new sheet?

    Filed under: ,
    • Post Points: 21
  • Wed, Jun 18 2008 4:37 PM In reply to

    • Nick Hodge
    • Top 10 Contributor
    • Joined on Sun, Dec 23 2007
    • Southampton
    • Posts 234
    • Points 3,866
    • MVP

    Re: Copying VBA from one sheet to another programatically

    MrPogle

    That's a whole huge subject with references that need to be set and a lack of good documentation. A good start would be here

    http://www.cpearson.com/excel/vbe.aspx

    Regards
    Nick Hodge
    Microsoft MVP, Excel
    Southampton, UK

    Filed under:
    • Post Points: 21
  • Wed, Jun 18 2008 5:31 PM In reply to

    • MrPogle
    • Top 50 Contributor
    • Joined on Tue, May 27 2008
    • Posts 5
    • Points 57

    Re: Copying VBA from one sheet to another programatically

    Thanks. I also found:

    http://www.mrexcel.com/forum/showthread.php?t=322504

    useful from which I tried the following:

    Sub WriteCode(str_Dest As String)
        Dim chrt As Chart, str_Code As String
        Set chrt = Sheets(str_Dest)
        str_Code = "Private Sub Chart_BeforeRightClick(Cancel As Boolean)" & vbCrLf
        str_Code = str_Code & "Cancel = True" & vbCrLf
        str_Code = str_Code & "CommandBars(""myShortcutBar"").ShowPopup" & vbCrLf
        str_Code = str_Code & "End Sub" & vbCrLf
        str_Code = str_Code & "Private Sub Chart_Activate()" & vbCrLf
        str_Code = str_Code & "CreateShortcutMenu" & vbCrLf
        str_Code = str_Code & "End Sub" & vbCrLf
        With ActiveWorkbook.VBProject.VBComponents(chrt.CodeName).CodeModule
            .InsertLines 1, str_Code
        End With
    End Sub

     

    It worked fine when I single stepped it one at a time but when I put it into my project I consistently got an Automation Error.

    • Post Points: 5
  • Wed, Jun 18 2008 5:34 PM In reply to

    • MrPogle
    • Top 50 Contributor
    • Joined on Tue, May 27 2008
    • Posts 5
    • Points 57

    Re: Copying VBA from one sheet to another programatically

    Actually, forget the above. The rest of my code manipulates pivot tables attached to a chart. When I put the procedure call (WriteCode("MyChart")) after the above, it works fine!

    • Post Points: 5
Page 1 of 1 (4 items) | RSS
Copyright Excel User Group and the relevant contributors, 2008. All Trade Marks acknowledged. This site is a peer-to-peer site and NOT affiliated in any way to Microsoft Corp. All rights reserved.