Results 1 to 8 of 8

Thread: Copying VBA code from one workbook into another

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    7

    Copying VBA code from one workbook into another

    Hi guys. I was wondering if anyone could help me out with a problem I am having. I'm not even sure if it's possible to do this but here's my situation:

    I have an existing Excel workbook with VBA code attached to the spreadsheets in it. I also have a button on the spreadsheet that when clicked generates an entirely new workbook file.

    What I was wondering is if it is possible to copy the VBA code that is in the existing workbook file into the newly generated workbook file that is generated when the button is clicked? Basically when the button is clicked, I want the new workbook file to be created and have it automatically contain the code from the previous workbook. Is this even possible? I appreciate any help I can get. Thanks.

  2. #2
    Addicted Member sweet_dreams's Avatar
    Join Date
    Apr 2005
    Location
    Poland, Lodz
    Posts
    189

    Re: Copying VBA code from one workbook into another

    maybe you should think about creating workbook template. while creating a new workbook based onm this template you will have all macros, formulas and so on which exist in your template.

    regards,
    sweet_dreams
    using VB 2010 .NET Framework 4.0; MS Office 2010; SQL Server 2008 R2 Express Edition | Remember to mark resolved threads and rate useful posts.

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Copying VBA code from one workbook into another

    You can read vba macro code from within vba or from outside using vb6 or .net.
    To basically read it using vba...

    VB Code:
    1. Application.VBE.ActiveVBProject.VBE.CodePanes.Item(1).CodeModule.Lines(1, 1)
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    Addicted Member
    Join Date
    Jan 2006
    Location
    Montreal, Canada
    Posts
    152

    Re: Copying VBA code from one workbook into another

    if your code is in a module you can use this

    VB Code:
    1. Sub CopyModule(SourceWB As Workbook, strModuleName As String, _
    2.     TargetWB As Workbook)
    3. ' copies a module from one workbook to another
    4. ' example:
    5. ' CopyModule Workbooks("Book1.xls"), "Module1", Workbooks("Book2.xls")
    6. Dim strFolder As String, strTempFile As String
    7.     strFolder = SourceWB.Path
    8.     If Len(strFolder) = 0 Then strFolder = CurDir
    9.     strFolder = strFolder & "\"
    10.     strTempFile = strFolder & "~tmpexport.bas"
    11.     On Error Resume Next
    12.     SourceWB.VBProject.VBComponents(strModuleName).Export strTempFile
    13.     TargetWB.VBProject.VBComponents.Import strTempFile
    14.     Kill strTempFile
    15.     On Error GoTo 0
    16. End Sub

    and if your code is in the sheet.
    Simply copy the sheet to the new workbook and the code will follow

  5. #5
    Addicted Member
    Join Date
    Jan 2006
    Location
    Montreal, Canada
    Posts
    152

    Re: Copying VBA code from one workbook into another

    also in order to use VbComponent from my example above, you have to check on box

    Tools --> Macro --> Security --> Trusted Sources
    and check the Trust Acces to Visual Basic Project

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Copying VBA code from one workbook into another

    I also have a CodeBank thread on this - http://www.vbforums.com/showthread.php?t=313861

    Good idae of exporting/importing the module. The only thing would be if there is a userform.

    Hmm...
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Addicted Member
    Join Date
    Jan 2006
    Location
    Montreal, Canada
    Posts
    152

    Re: Copying VBA code from one workbook into another

    According to Microsoft, it is possible to export/import userform too.

    Here is the link http://support.microsoft.com/?kbid=292037 in the Import and Export VBComponents section

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Copying VBA code from one workbook into another

    Thanks, I never had the need to export a userform, but my codebank example iterated through all vba objects. Guess I should have tested that part too and I would have seen it work.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width