Duplicate workbook then delete named modules from the duplicate workbook

StillUnderstanding

Board Regular
Joined
Jan 30, 2021
Messages
80
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello,

I wonder if anyone can help me with a problem I am stuck on. I am using code that allows me to duplicate my workbook and then save it to another location. What I am trying to do is delete some of the modules from the duplicated workbook before saving.

I am using the below code and everything works apart from the module deleting part. Can anyone please help me with this please?


VBA Code:
 'Create duplicate in temp folder.
    ThisWorkbook.SaveCopyAs strTempFolderPath & "2022a.xlsb"
    
    Application.Wait (Now + TimeValue("0:00:05"))
    '2) ----------> Open the duplicate file
    Set wbDuplicate = Workbooks.Open(strTempFolderPath & "2022a.xlsb")
  
   Dim vbCom As Object

   'Disabling the alert message
Application.DisplayAlerts = False
'Ignore errors
On Error Resume Next
'Delete the component
wb.VBProject.VBComponents.Remove wb.VBProject.VBComponents("test", "Module111", "Module3")
On Error GoTo 0
'Enabling the alert message
Application.DisplayAlerts = True

         On Error GoTo 0
    'Next
    
    '3) ----------> Close all tabs except the following:
     '- Import Core Data
    '- SendMail
    '- Test CP Weekly
    Worksheets("Lookup").Unprotect

    For Each shSheet In wbDuplicate.Sheets
        If shSheet.Name <> "TAB 1" And shSheet.Name <> "TAB2" And shSheet.Name <> "Tab3" Then
            shSheet.Visible = xlVeryHidden
            Sheets("Tab3").Visible = False
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try the following ...

VBA Code:
    Dim modulesToRemove As Variant
    Dim i As Long
    
    modulesToRemove = Array("test", "Module111", "Module3")
    
    On Error Resume Next
    For i = LBound(modulesToRemove) To UBound(modulesToRemove)
        With wb.VBProject.VBComponents
            .Remove .Item(modulesToRemove(i))
        End With
    Next i
    On Error GoTo 0

Hope this helps!
 
Upvote 0
Solution
Thanks @Domenic for your help with this.

I have changed the code to the below and the macro continues to run with no issue but it's not deleting the macros in the duplicated workbook.

I did have the macro code protected so I even took that off first to see if that was the issue and its not.

I'm totally as a loss on this.

VBA Code:
 'Create duplicate in temp folder.
    ThisWorkbook.SaveCopyAs strTempFolderPath & "2022a.xlsb"
    
    Application.Wait (Now + TimeValue("0:00:05"))
    '2) ----------> Open the duplicate file
    Set wbDuplicate = Workbooks.Open(strTempFolderPath & "2022a.xlsb")
 
   Dim modulesToRemove As Variant
    Dim i As Long
    
    modulesToRemove = Array("test", "Module1", Macro2)
    
    On Error Resume Next
    For i = LBound(modulesToRemove) To UBound(modulesToRemove)
        With wb.VBProject.VBComponents
            .Remove .Item(modulesToRemove(i))
        End With
    Next i
    On Error GoTo 0
    
    '3) ----------> Close all tabs except the following:
     '- Import Core Data
    '- SendMail
    '- Test CP Weekly
    Worksheets("Lookup").Unprotect

    For Each shSheet In wbDuplicate.Sheets
        If shSheet.Name <> "TAB 1" And shSheet.Name <> "TAB2" And shSheet.Name <> "Tab3" Then
            shSheet.Visible = xlVeryHidden
            Sheets("Tab3").Visible = False
 
Upvote 0
First, I didn't notice it before, but I see that Macro2 is listed in the array. What is it? It's not a module name, is it? If so, it should be enclosed within quotes. Or is it a variable that contains the name of a module?

Secondly, comment out or temporary delete On Error Resume Next, and run the macro again. What happens this time? Do you get an error? If so, which one, and on which line?
 
Upvote 0
First, I didn't notice it before, but I see that Macro2 is listed in the array. What is it? It's not a module name, is it? If so, it should be enclosed within quotes. Or is it a variable that contains the name of a module?

Secondly, comment out or temporary delete On Error Resume Next, and run the macro again. What happens this time? Do you get an error? If so, which one, and on which line?
Sorry, that was my error with Macro2, should be "Marco2". That's the name of the module.

I commented out the Error Resume Next and it stopped with he following errer:- With wb.VBProject.VBComponents
 
Upvote 0
You didn't say which error message you're getting, but I'm assuming it's . . .

VBA Code:
Rune time error '1004':

Programmatic access to Visual Basic Project is not trust

If so, you'll need to allow access to the Visual Basic Project, as follows . . .

VBA Code:
On the Ribbon >> under the Developer tab >> in the Code group >> Macro Security >> select/click Trust access to the VBA project object model

Does this help?
 
Upvote 0
You didn't say which error message you're getting, but I'm assuming it's . . .

VBA Code:
Rune time error '1004':

Programmatic access to Visual Basic Project is not trust

If so, you'll need to allow access to the Visual Basic Project, as follows . . .

VBA Code:
On the Ribbon >> under the Developer tab >> in the Code group >> Macro Security >> select/click Trust access to the VBA project object model

Does this help?
Thanks @Domenic Oh, yes correct the error was 1004 so that helps me to the point that I now get the following error
Rich (BB code):
Run-time error '438'

Object doesn't support the property or method
When I debug it brings me here:- With wbDuplicate.VBProject.VBComponents.VBProject.VBComponents
 
Upvote 0
With wbDuplicate.VBProject.VBComponents.VBProject.VBComponents
I don't know where you got that line from, but take another look at the code I provided you with, and you'll see the correct syntax . . .
 
Upvote 0
I don't know where you got that line from, but take another look at the code I provided you with, and you'll see the correct syntax . . .
Ok, got all the code as expected and corrected all of the above but I am now getting a
VBA Code:
runtime error 424 
Object required

Not sure why but its just not running.
 
Upvote 0
I don't know where you got that line from, but take another look at the code I provided you with, and you'll see the correct syntax . . .
Ok, thank you I have it working. I removed the password from the VBA and it's working.

How do I run it so it disables the password in the project then re-enables it? Can this be done?
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,428
Members
449,083
Latest member
Ava19

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