+ Reply to Thread
Results 1 to 11 of 11

Add vb code to a new workbook using a macro

  1. #1
    Registered User
    Join Date
    12-03-2010
    Location
    Minnesota
    MS-Off Ver
    Excel 2003
    Posts
    16

    Add vb code to a new workbook using a macro

    Please Login or Register  to view this content.
    This is the code I am using now. I am creating a new workbook with the data from this text file and inputting the formulas that I need. The issue that I am having is that I want to be able to add the code below to the new sheet on it's creation and I have not been able to get it to work. Thanks for your help.

    Please Login or Register  to view this content.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Add vb code to a new workbook using a macro

    Hello Alagard,

    This macro will copy the contents of a text file to the active sheet code module of the workbook. You will need to copy your macro and save it as a text file. Change the file path in the macro to this location. Call the macro before you close the workbook and save it.
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    12-03-2010
    Location
    Minnesota
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Add vb code to a new workbook using a macro

    I run into an issue here. I am creating the new worksheet in my macro and filling it with data and formulas at that step. Looking at this, can I incorporate this into the code at the beginning to do this all in one step?

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Add vb code to a new workbook using a macro

    Hello Alagard,

    I have added the macro call to your code. You will still need to copy and paste your sound macro into Notepad and save it. Once you have done that, you will need to change the path in the macro I wrote to your file's location.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    12-03-2010
    Location
    Minnesota
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Add vb code to a new workbook using a macro

    I get an error when I try to run that.

    Programmatic access to Visual Basic Project is not trusted

    the debugger then highlights this line
    Set VBproj = Wkb.VBProject

    Thanks for the help.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Add vb code to a new workbook using a macro

    Hello Alagard,

    If you are running this code on you r own machine, you should not be getting this error. However, if you are attempting to change another workbook on someone else's machine you will.

  7. #7
    Registered User
    Join Date
    12-03-2010
    Location
    Minnesota
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Add vb code to a new workbook using a macro

    sure the workbook and all of the files are on a shared drive.

  8. #8
    Registered User
    Join Date
    12-03-2010
    Location
    Minnesota
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Add vb code to a new workbook using a macro

    So I checked the box for trusted access and that step goes okay however now the next step gives an error

    Run-time error '9':
    Subscript out of range
    Set VBmod = VBproj.VBComponents(ActiveSheet.Name).CodeModule

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Add vb code to a new workbook using a macro

    Hello Alagard,

    The error indicates the name of the module was not found. Which doesn't make sense because a workbook must have at least one worksheet in it and it is looking for the Active Sheet. Can you post the workbook for review?

  10. #10
    Registered User
    Join Date
    12-03-2010
    Location
    Minnesota
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Add vb code to a new workbook using a macro

    I attached the whole workbook here, you will need to create a text file to input some data but everything else should be there.
    Attached Files Attached Files

  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Add vb code to a new workbook using a macro

    Hello Alagard,

    Based on your previous post and code, you were opening a new workbook and I included a call to the macro at this time. The target workbook would be the active workbook (new workbook). The code will be written into the Sheet module of the active sheet in the active workbook.

    I assume that both workbooks are open in the same instance of Excel. If the target workbook is open in another instance of Excel the macro will error. But, the error would be different than the one you are getting. I can not reproduce the error on my machine.

+ 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