Copying VBA Modules

Rusty_The_Kid

New Member
Joined
Apr 18, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I am relatively new to Excel VBA, and I began using AI to get started this morning, but I figure you all have AI out-gunned. I want to use VBA code to copy two existing modules, module1 and module1, within my workbook, and create two new duplicate modules, except or course with the next two available higher module numbers; perhaps module13 and module14, but maybe higher module numbers depending on how many modules exist in my workbook at the time. Below is the VBA code I tried running:
VBA Code:
Sub CopyModules()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim i As Integer
    Set wb = ThisWorkbook
    Set ws = wb.Sheets(wb.Sheets("Auto-Cloning").Range("B3").Value)
    For i = 1 To 2
        wb.VBProject.VBComponents("Module" & i).Export "C:\Temp\Module" & i & ".bas"
        wb.VBProject.VBComponents.Import "C:\Temp\Module" & i & ".bas"
        wb.VBProject.VBComponents("Module" & i + 2).Name = "Module" & i + 4
    Next i
End Sub

I get "Run-time error '50035': Method 'Export' of object '_VBComponent' failed, with the "wb.VBProject.VBComponents("Module" & i).Export "C:\Temp\Module" & i & ".bas"" line highlighted.

I've already enabled all macros and checked the box next to "Trust access to the VBA project object model".

Thanks in advance!
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I thought that I would try a different approach of creating a new module, then copy everything from module1 into the new module. I thought that way the code wouldn't need to determine what the highest numbered module was. I tried the following code:

VBA Code:
Sub CopyModule()
    Dim NewModule As Object
    Dim OldModule As Object
    Set OldModule = ThisWorkbook.VBProject.VBComponents("Module1")
    Set NewModule = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_StdModule)
    NewModule.Name = "NewModule"
    NewModule.CodeModule.AddFromString OldModule.CodeModule.Lines(1, OldModule.CodeModule.CountOfLines)
End Sub

I got "Run-time error '440': Method 'Add' of object '_VBComponents' failed, with the following line of code highlighted: Set NewModule = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_StdModule)
 
Upvote 0
I've already enabled all macros and checked the box next to "Trust access to the VBA project object model"
And added the reference to Microsoft Visual Basic for Applications Extensibility 5.3?
 
Upvote 0
Solution
That did it! I opened up the VBA Editor, Tools, References, and selected Microsoft Visual Basic for Application Extensibility 5.3, OK

And, the code created a new module titled "NewModule", and copied all of the Module1 contents into it.

Thanks much! I didn't even know such references existed. I suppose I will mark this as solved, and start new threads as I run into my upcoming roadblocks during this VBA project.
 
Upvote 0
Great! I will definitely read the article. I sure appreciate the help and patience offered to us newbies who may have a tendency to dive-into VBA programming, without prior associated classes or research! You facilitate important efficiency.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top