+ Reply to Thread
Results 1 to 15 of 15

Add a Macro to a New Workbook Using VBA

  1. #1
    Registered User
    Join Date
    05-10-2018
    Location
    Pennsylvania
    MS-Off Ver
    Office 365
    Posts
    43

    Add a Macro to a New Workbook Using VBA

    Hello!

    This is a very frustrating macro I am working on.

    So now I need to find a way to copy or create a new module and macro in a new workbook using vba. So I need to create a button to update data but I need to add the macro to the new workbook and tie it into the button. So I can create the button but I can't figure out how to add or copy a macro from the original workbook to the new workbook or to create just the Sub I need from the original workbook to the new workbook.

    Any help would be great!

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Add a Macro to a New Workbook Using VBA

    Hi,

    Does the following help

    Please Login or Register  to view this content.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    05-10-2018
    Location
    Pennsylvania
    MS-Off Ver
    Office 365
    Posts
    43

    Re: Add a Macro to a New Workbook Using VBA

    Richard,

    The issue I have is that the macro I am running creates the new workbook but it will not be saved at that time as it is not a localized document. So I'm not sure how to select the New Workbook Name as it can vary depending on if other workbooks were run or if this is run twice. So it may be Book1, Book2, etc.

    Thanks!

  4. #4
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Add a Macro to a New Workbook Using VBA

    @Allienne You could set the new workbook as a workbook object at time of creation and then pass it on as a parameter to the Sub CopyModule() sub. That way you don't need the name anymore.

    @Richard sorry for jumping in on something you have handled, I know I usually don't post much after hours and since you are in the UK I thought you might not be on again today.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  5. #5
    Registered User
    Join Date
    05-10-2018
    Location
    Pennsylvania
    MS-Off Ver
    Office 365
    Posts
    43

    Re: Add a Macro to a New Workbook Using VBA

    How do I set a workbook object?

  6. #6
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Add a Macro to a New Workbook Using VBA

    How are you creating the "new" workbook?
    Set wbNew = Workbooks("your new workbook name") is the line of code where Richard Sets the workbook object, where wbNew becomes the workbook that has the name in the quotes.

    Please Login or Register  to view this content.
    would make the new workbook wbNew, then depending on how you have integrated Richard's code (Call CopyModule?) you could change it to:

    Please Login or Register  to view this content.
    And then just comment out Set wbNew = Workbooks("your new workbook name")
    Last edited by Arkadi; 01-21-2019 at 03:02 PM. Reason: forgot to put "As Workbook" in the call to copymodule

  7. #7
    Registered User
    Join Date
    05-10-2018
    Location
    Pennsylvania
    MS-Off Ver
    Office 365
    Posts
    43

    Re: Add a Macro to a New Workbook Using VBA

    I'm creating the workbook by moving a Sheet from the original workbook to a new workbook.

  8. #8
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Add a Macro to a New Workbook Using VBA

    Ok I assume that is workheets("sheet name").copy?
    When you copy the sheet, I presume (as is normal) that the new book is the active one as soon as it gets created. So instead of

    Please Login or Register  to view this content.
    you could, next line after the copy, put:

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    05-10-2018
    Location
    Pennsylvania
    MS-Off Ver
    Office 365
    Posts
    43

    Re: Add a Macro to a New Workbook Using VBA

    For some reason, I keep getting Compile error: User-defined type not defined for the
    Please Login or Register  to view this content.

  10. #10
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Add a Macro to a New Workbook Using VBA

    In the VBA editor, go to Tools->References and put a checkmark next to Microsoft Visual Basic for Applications Extensibility x.x
    In my case x.x is 5.3 (office 2016) not sure if your version number is the same but that should not matter.

  11. #11
    Registered User
    Join Date
    05-10-2018
    Location
    Pennsylvania
    MS-Off Ver
    Office 365
    Posts
    43

    Re: Add a Macro to a New Workbook Using VBA

    I don't have that option. It is greyed out for me.
    reference greyed out.jpg

  12. #12
    Registered User
    Join Date
    05-10-2018
    Location
    Pennsylvania
    MS-Off Ver
    Office 365
    Posts
    43

    Re: Add a Macro to a New Workbook Using VBA

    I got it to work, I had to restart the VB Editor. But now it says Run-time error '1004': Programmatic access to Visual Basic Project is not trusted. no idea what that means.

  13. #13
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Add a Macro to a New Workbook Using VBA

    I was going to say.... you need to stop code from running, can't add it while it is in break mode (meaning error line still highlighted).

    For your next issue. In Excel, go to File -> Options -> Trust Center
    Click on Trust Center Settings, then choose Macro Settings
    There put a checkmark next to "Trust access to the VBA project object model"

  14. #14
    Registered User
    Join Date
    05-10-2018
    Location
    Pennsylvania
    MS-Off Ver
    Office 365
    Posts
    43

    Re: Add a Macro to a New Workbook Using VBA

    It works!! Thank you so much!!

  15. #15
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Add a Macro to a New Workbook Using VBA

    Yay!

    Thanks for marking the thread as solved, and letting us know how it went

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Can a Macro in a Workbook open and run a macro found in the other Workbook
    By Bobbbo in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-05-2016, 01:53 PM
  2. Personal Macro Workbook interference with other workbook macro(s)
    By Xanlithe in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-10-2015, 09:41 AM
  3. Replies: 1
    Last Post: 10-10-2015, 01:00 AM
  4. [SOLVED] VBA to open new workbook and run macro from new workbook, which closes old workbook
    By Rerock in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-26-2014, 10:45 AM
  5. [SOLVED] Macro to find data in source workbook and copy paste to target workbook
    By D.Lovell in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 04-23-2014, 06:21 AM
  6. [SOLVED] Save the split the workbook file type as Excel Binary Workbook From Run Macro
    By breadwinner in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-25-2013, 05:09 AM
  7. Replies: 0
    Last Post: 07-27-2011, 09:48 PM

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