Rusty_The_Kid
New Member
- Joined
- Apr 18, 2023
- Messages
- 4
- Office Version
- 365
- Platform
- 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:
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!
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!