|
-
Mar 15th, 2006, 09:29 PM
#1
Thread Starter
New Member
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.
-
Mar 16th, 2006, 01:08 AM
#2
Addicted Member
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. 
-
Mar 16th, 2006, 02:13 AM
#3
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:
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Mar 16th, 2006, 10:09 AM
#4
Addicted Member
Re: Copying VBA code from one workbook into another
if your code is in a module you can use this
VB Code:
Sub CopyModule(SourceWB As Workbook, strModuleName As String, _
TargetWB As Workbook)
' copies a module from one workbook to another
' example:
' CopyModule Workbooks("Book1.xls"), "Module1", Workbooks("Book2.xls")
Dim strFolder As String, strTempFile As String
strFolder = SourceWB.Path
If Len(strFolder) = 0 Then strFolder = CurDir
strFolder = strFolder & "\"
strTempFile = strFolder & "~tmpexport.bas"
On Error Resume Next
SourceWB.VBProject.VBComponents(strModuleName).Export strTempFile
TargetWB.VBProject.VBComponents.Import strTempFile
Kill strTempFile
On Error GoTo 0
End Sub
and if your code is in the sheet.
Simply copy the sheet to the new workbook and the code will follow
-
Mar 16th, 2006, 10:14 AM
#5
Addicted Member
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
-
Mar 16th, 2006, 11:54 AM
#6
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Mar 16th, 2006, 12:46 PM
#7
Addicted Member
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
-
Mar 16th, 2006, 01:18 PM
#8
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|