+ Reply to Thread
Results 1 to 9 of 9

create new workbook and sheet with vba code in it

  1. #1
    Registered User
    Join Date
    03-30-2011
    Location
    Los Angeles, Ca
    MS-Off Ver
    Excel 2003
    Posts
    8

    create new workbook and sheet with vba code in it

    Hello everyone this is my first post here and I hope I am explaining myself well. I have a macro that creates a new workbook with sever worksheets in it. The new worksheets get formatted with color, words and math formulas in it. This part all works perfect. The part that I can not figure out it how to add VBA to a particular sheet or sheet object (not a userform or module) . I am trying to add the code :
    Please Login or Register  to view this content.
    So if some clicks in cell D1 on a worksheet it will trigger a userform (this userform opens up the calendar so that they can select a date, that then populates the cell, this part works fine if I manually enter the code by right clicking on the tab, selecting view code and entering it. So again how do I create this new tab/worksheet with this code already in it.

    I am in excel 2007.

    Thanks in advance
    Eric
    Last edited by pike; 03-31-2011 at 12:10 AM. Reason: add code tags

  2. #2
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,255

    Re: create new workbook and sheet with vba code in it

    Hi Eric,

    The simplist way is to have a worksheet template set up. Let's say Sheet1 is an empty sheet with that Worksheet_SelectionChange code you posted in it's code module. Then we can use the Worksheet.Copy method to create a new sheet:
    Please Login or Register  to view this content.
    The created sheet will have the event handler code in too.


    A second option would be to create a new sheet using Worksheets.Add and then programmatically add code to its class module.
    Hope that helps,

    Colin

    RAD Excel Blog

  3. #3
    Registered User
    Join Date
    03-30-2011
    Location
    Los Angeles, Ca
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: create new workbook and sheet with vba code in it

    Colin,
    I can not do the sheet copy because I have a macro that creates a new workbook with several sheets. So there is no sheet to copy, they are all new.

    Also, I am not sure what you mean by programmatically add code? Can you elaborate,

    Thank you

  4. #4
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,255

    Re: create new workbook and sheet with vba code in it

    Hi Eric,
    I can not do the sheet copy because I have a macro that creates a new workbook with several sheets. So there is no sheet to copy, they are all new.
    That's even better then.... why don't you just set up an entire workbook template which you can use as a basis for creating the new workbook?

    Please Login or Register  to view this content.
    Have you considered how this new workbook will be able to show the userform which is contained in a different workbook, or is that the next challenge?

  5. #5
    Registered User
    Join Date
    03-30-2011
    Location
    Los Angeles, Ca
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: create new workbook and sheet with vba code in it

    Currently I have a dashboard workbook which creates a new workbook for every project. All of the macros and userforums reside in the dashboard workbook. All of the project workbook data feed the dashboard workbook. Also each project workbook consists of 15 pages all with there own formatting and all with there own userforums and macros that all reside in the dashboard workbook. I believe this only works because the dashboard workbook is always open when a project workbook is open because you can only open a project workbook or create a project workbook from with in the dashboard workbook. I have not used a workbook template before, but I am looking into it. Is there another way to add the code to a worksheet when you create it with Sheets.Add.Name = “sheet name”

    -Eric

  6. #6
    Registered User
    Join Date
    03-30-2011
    Location
    Los Angeles, Ca
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: create new workbook and sheet with vba code in it

    ok making some progress, I found this line of code that allows me to create a new worksheet and adds a Worksheet_SelectionChange(ByVal Target As Range) to the new worksheet named "test". Now I just need to figure out how to add the line:

    Please Login or Register  to view this content.
    to the event.

    Please Login or Register  to view this content.
    Last edited by runTNT; 04-04-2011 at 07:33 PM.

  7. #7
    Registered User
    Join Date
    03-30-2011
    Location
    Los Angeles, Ca
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: create new workbook and sheet with vba code in it

    Ok figured it out, for who ever wants to know and colin thanks for the help

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    03-30-2011
    Location
    Los Angeles, Ca
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: create new workbook and sheet with vba code in it

    ok, so now my code gets inserted and works, but when I use the

    Please Login or Register  to view this content.
    it does not save the new code.... any reason why that is?

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: create new workbook and sheet with vba code in it

    ActiveWorkbook refers to the workbook currently open. If the code is not in the active workbook then it will save the currently active workbook, if you want to save the workbook containing the code use ThisWorkBook instead.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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