+ Reply to Thread
Results 1 to 11 of 11

Macro to export and import a module and assigning different macro automatically

  1. #1
    Forum Contributor
    Join Date
    01-21-2010
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 and 2007
    Posts
    105

    Smile Macro to export and import a module and assigning different macro automatically

    I have had a go at this but I have clearly not got it right.

    The code I have used is:
    Please Login or Register  to view this content.
    I would like the sheets Purchase order and Continuation sheet to be copied and saved which the Copy_Save Macr does however when this does this I want module 2 in my VBE to go with the new sheet.

    I'm sure it can work but i have not done it right .

    Any help gratefully recieved.

    Thanks
    Last edited by Libster78; 01-28-2010 at 06:46 AM.

  2. #2
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Macro to export and import a module

    Hi Libster78
    I use this
    where CopyAMouldle( wkb From, wkbTo , "Module2")

    Please Login or Register  to view this content.
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  3. #3
    Forum Contributor
    Join Date
    01-21-2010
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 and 2007
    Posts
    105

    Re: Macro to export and import a module

    I can't get this to work. I want it to only export Module 2 of the 2 modules and ignore module 1.

    So I click Print button on original workbook, it prints the sheets requestes, transfers specific data onto a 3rd sheet, copies the printed sheets in to a new workbook, saves it.

    I want to be able to go into this newly formed workbook click print button as before the difference being that it prints, transfers the data onto my original workbook summary sheet, then saves.

    The module one for original workbook is:
    Please Login or Register  to view this content.
    The module 2 which I want to export is:
    Please Login or Register  to view this content.
    Can this work with some tweeking?

    Thanks

  4. #4
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Macro to export and import a module

    you need to set the variables and run call the sub

    Please Login or Register  to view this content.
    eg
    Please Login or Register  to view this content.

  5. #5
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Macro to export and import a module

    Still persevering with your little project I see.

    Pike's suggestion should work fine but a more basic solution would be:

    Please Login or Register  to view this content.
    You could just call that after you have copied the sheets to the new workbook.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  6. #6
    Forum Contributor
    Join Date
    01-21-2010
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 and 2007
    Posts
    105

    Re: Macro to export and import a module

    Struggling with my little project but determined to make it work.

    The issues i have is I cannot name the new workbook as it is an automatically created name i.e, Book2, Book3, so it is ever changing.

    I'll give these a try first.

    I'll get there with all your help.

    Thanks guys

    Libby

  7. #7
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Macro to export and import a module

    The code I posted should get round that as it transfers the module from the workbook that you are running the code (ThisWorkbook) from and the ActiveWorkbook should always be the one that is created when the sheets are copied to the new workbook.

    Edit: not tested but you could probably use Pike's function like this to get round that as well:

    Please Login or Register  to view this content.
    Dom
    Last edited by Domski; 01-27-2010 at 08:43 AM.

  8. #8
    Forum Contributor
    Join Date
    01-21-2010
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 and 2007
    Posts
    105

    Re: Macro to export and import a module

    Ok that is working only my print button is automatically assigned to macro in module 1 and not to the macro in module 2 in the new sheet.

    I.e. on original sheet PrintInvoice is the macro assigned to the button. The new sheet is created with module2 transferred but when print button is clicked it errors.

    Now I need to automatically reassign the macro to this button.

    I know I am a pain. I really appreciate all the help.

    Thanks

  9. #9
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Macro to export and import a module

    You can assign the macro to run to a button like this:

    Please Login or Register  to view this content.
    Dom

  10. #10
    Forum Contributor
    Join Date
    01-21-2010
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 and 2007
    Posts
    105

    Re: Macro to export and import a module and assigning different macro automatically

    Thanks to all again for all the help

    I habe new issues but will post these separately.

    Thanks again.

    Libby

  11. #11
    Registered User
    Join Date
    08-22-2011
    Location
    Berlin
    MS-Off Ver
    Excel 2010
    Posts
    51

    Re: Macro to export and import a module and assigning different macro automatically

    I am not sure but it looks like a try to export/import a Data Module (that is the workbook's and the worksheets own code) which is impossible. The only Code Modules which can be ex- and imported are Standard Code Module (.bas), Class Module (.cls) and UserForm (.frm, .frx). The only way to still ex-/import code in Data Modules is to move the code into a Standard Module and just leave the call to it on the original place. So for example in a Standard Module called modWrkbk:
    Please Login or Register  to view this content.
    and in the Workbook itself just leave:
    Please Login or Register  to view this content.
    The same with code in a Worksheet which may be moved e.g. into a Standard Module called modWshxxx:

    Please Login or Register  to view this content.
    and in the Worksheet just leave:
    Please Login or Register  to view this content.
    This way Data Modules will no longer contain any code but the call to another Standard Module's public procedure. Data Modules in the end contain nothing but the event procedures ..._Open, ..._Change, ...._BeforeDoubleClick, etc.

    Last but not least I recommend to provide each Worksheet with a proper object name (e.g. wshXxxx) and use this throughout the project. This way you not only become independent from the name of the tab which can then be anything meaningfull for the user and even renamed when applicable, but also end up with a much clearer code because instead of
    Please Login or Register  to view this content.
    You will say
    Please Login or Register  to view this content.
    Last edited by Warbe; 11-14-2015 at 06:51 PM.

+ 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