Using VBA to remove a module

mikechambers

Active Member
Joined
Apr 27, 2006
Messages
397
I have the following code in Workbook C to perform the following steps:

1. Export Module1 to a BAS file from Workbook A
2. Delete Module1 from Workbook B
3. Import BAS file into Workbook B as Module1

Code:

Dim FName As String, vbCom As Object
With Workbooks("Workbook A.xls")
FName = "C:\code.bas"
.VBProject.VBComponents("Module1").Export FName
End With
Set vbCom = Workbooks("Workbook B.xls").VBProject.VBComponents
vbCom.Remove VBComponent:=vbCom.Item("Module1")
vbCom.Import FName
Kill FName

The problem is after the Remove line, Module1 still shows up in Workbook B, so that when it imports FName, it does so as Module11. BUT, if I end the macro after the remove line, Module1 disappears. So it seems like a refresh issue or something. It is not actually removed until the macro is finished running? Does that make sense? I tried Application.Screenupdating=True after the Remove line, but didn't fix it. Any ideas???
 
Do you actually want that code to run, if you're changing the module?
 
Upvote 0

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I have the following code in Workbook C to perform the following steps:

1. Export Module1 to a BAS file from Workbook A
2. Delete Module1 from Workbook B
3. Import BAS file into Workbook B as Module1

Hi Mike

If I understand correctly you just want to copy the code from Module1 in workbook A to WorkbookB.

I don't think you need to export/import, create a file, etc. Maybe it's easier to copy the code directly:

Code:
Public Sub CopyModuleText()
Dim VBCodMod1 As Object, VBCodMod2 As Object
 
Set VBCodMod1 = Workbooks("Workbook A.xls").VBProject.VBComponents("Module1").CodeModule
Set VBCodMod2 = Workbooks("Workbook B.xls").VBProject.VBComponents("Module1").CodeModule
 
VBCodMod2.DeleteLines 1, VBCodMod2.CountOfLines
VBCodMod2.AddFromString VBCodMod1.Lines(1, VBCodMod1.CountOfLines)
End Sub
 
Upvote 0
Yes - put:
Code:
application.enablevents = false
Before opening the workbooks. Make sure you set it back to true at the end of your routine.
 
Upvote 0
I tried just copying the code per PGC's suggestion, but it caused Excel to crash. I think it's for the same reasoning. I guess since it is running the Open code from Module1, it then can't alter that code in the same procedure. So all I had to do was disable the Open routine from running, using rorya's reply, Application.EnableEvents=False and it works beautifully.

Thanks to everyone for the help. I really appreciate it!!!
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,717
Members
448,985
Latest member
chocbudda

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