+ Reply to Thread
Results 1 to 4 of 4

Progmatically add code to worksheet_activate

  1. #1
    Luca
    Guest

    Progmatically add code to worksheet_activate

    Hi,

    I have a macro that creates a new worksheet, however when it does this i
    want it to add some code to the worksheet_Activate event of the new sheet.
    Assuming the new worksheet is called "NewYP" and the code i would like in
    activate_Sheet event is

    Private Sub Worksheet_Activate()

    Toolbars.YPBar

    End Sub

    How do i go about adding the code ?

    All help is appreciated

    Luca


  2. #2
    Chip Pearson
    Guest

    Re: Progmatically add code to worksheet_activate

    You can have VBA code write VBA code. See
    www.cpearson.com/excel/vbe.htm for more details.


    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com


    "Luca" <Luca@discussions.microsoft.com> wrote in message
    news:A39EF283-67DE-4D1B-BAEF-753E07316821@microsoft.com...
    > Hi,
    >
    > I have a macro that creates a new worksheet, however when it
    > does this i
    > want it to add some code to the worksheet_Activate event of the
    > new sheet.
    > Assuming the new worksheet is called "NewYP" and the code i
    > would like in
    > activate_Sheet event is
    >
    > Private Sub Worksheet_Activate()
    >
    > Toolbars.YPBar
    >
    > End Sub
    >
    > How do i go about adding the code ?
    >
    > All help is appreciated
    >
    > Luca
    >




  3. #3
    chijanzen
    Guest

    RE: Progmatically add code to worksheet_activate

    Luca:

    try,

    Sub Test()
    Dim sht As Worksheet
    Set sht = Sheets.Add
    sht.Name = "NewYP"
    With ThisWorkbook.VBProject.VBComponents.Item(sht.CodeName).CodeModule
    .InsertLines 1, "Private Sub Worksheet_Activate()"
    .InsertLines 2, ""
    .InsertLines 3, "Toolbars.YPBar"
    .InsertLines 4, ""
    .InsertLines 5, "End Sub"
    End With
    End Sub

    --
    天行健,君å*ä»¥è‡ªå¼·ä¸æ¯
    地勢坤,君å*ä»¥åŽšå¾·è¼‰ç‰©

    http://www.vba.com.tw/plog/


    "Luca" wrote:

    > Hi,
    >
    > I have a macro that creates a new worksheet, however when it does this i
    > want it to add some code to the worksheet_Activate event of the new sheet.
    > Assuming the new worksheet is called "NewYP" and the code i would like in
    > activate_Sheet event is
    >
    > Private Sub Worksheet_Activate()
    >
    > Toolbars.YPBar
    >
    > End Sub
    >
    > How do i go about adding the code ?
    >
    > All help is appreciated
    >
    > Luca
    >


  4. #4
    Chip Pearson
    Guest

    Re: Progmatically add code to worksheet_activate

    Your code will not work in the 'Require Variable Declaration'
    option is set to true, because line 1 will be 'Option Explicit'
    and your code will push this down. Instead, use CreateEventProc.

    Dim InsLine As Long
    Dim sht As Worksheet
    Set sht = Sheets.Add

    sht.Name = "NewYP"
    With ThisWorkbook.VBProject.VBComponents(sht.CodeName).CodeModule
    InsLine = .CreateEventProc("Activate", "Worksheet") + 1
    .InsertLines InsLine, "Toolbars.YPBar"
    End With




    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com



    "chijanzen" <chijanzen@discussions.microsoft.com> wrote in
    message
    news:0CB9F5C0-64AA-438B-BB71-D1779B5CD302@microsoft.com...
    > Luca:
    >
    > try,
    >
    > Sub Test()
    > Dim sht As Worksheet
    > Set sht = Sheets.Add
    > sht.Name = "NewYP"
    > With
    > ThisWorkbook.VBProject.VBComponents.Item(sht.CodeName).CodeModule
    > .InsertLines 1, "Private Sub Worksheet_Activate()"
    > .InsertLines 2, ""
    > .InsertLines 3, "Toolbars.YPBar"
    > .InsertLines 4, ""
    > .InsertLines 5, "End Sub"
    > End With
    > End Sub
    >
    > --
    > ???,???????
    > ???,???????
    >
    > http://www.vba.com.tw/plog/
    >
    >
    > "Luca" wrote:
    >
    >> Hi,
    >>
    >> I have a macro that creates a new worksheet, however when it
    >> does this i
    >> want it to add some code to the worksheet_Activate event of
    >> the new sheet.
    >> Assuming the new worksheet is called "NewYP" and the code i
    >> would like in
    >> activate_Sheet event is
    >>
    >> Private Sub Worksheet_Activate()
    >>
    >> Toolbars.YPBar
    >>
    >> End Sub
    >>
    >> How do i go about adding the code ?
    >>
    >> All help is appreciated
    >>
    >> Luca
    >>




+ 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