incorporating a macro import within another sub

Reddog94

Board Regular
Joined
Dec 20, 2011
Messages
52
The following code (blue font) finds specific worksheets in an active workbook, and copies them into a new workbook then names the new workbook the same name of the worksheet. It all works perfectly.

The second code below (red font) imports some vb into the new workbook. It works fine if I manually open the new workbook and run it in the immediate window. However, I would like to incorporate the red code into the blue code so it all runs as one. I've tried inserting the red line after the "ws.copy after" line and after the ".saveas" line, but no dice.

I think it is because I'm not making the new book active (but that is an uneducated guess). Does anyone know what I'm doing wrong?


For Each ws In wb.Worksheets
If UCase(Left(ws.Name, 2)) = "CC" Then
Set NewBook = Workbooks.Add
With NewBook
.Title = ws.Name
ws.Copy After:=NewBook.Worksheets("Sheet3")

For Each ws2 In NewBook.Worksheets
If ws2.Name <> ws.Name Then
ws2.Delete
End If

Next
.SaveAs Filename:="C:\Users\lmcginle\Desktop\" & ws.Name
.Close SaveChanges:=True
End With
End If
Next

wb.Activate

Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub


Application.VBE.ActiveVBProject.VBComponents.Import ("T:\Finance Mgmt\Reporting\MacroExport.bas")
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Can you be more specific in "setting a reference"? I followed the directions in the link you sent (making sure both the proper options to allow the import were enabled), but it still isn't working. Do I need to actually reference something in the code that I'm not understanding?

Thanks for the response!
 
Upvote 0
Can you be more specific in "setting a reference"?
From the link...
"First, you need to set an reference to the VBA Extensibililty library. The library contains the definitions of the objects that make up the VBProject. In the VBA editor, go the the Tools menu and choose References. In that dialog, scroll down to and check the entry for Microsoft Visual Basic For Applications Extensibility 5.3. If you do not set this reference, you will receive a User-defined type not defined compiler error."


Can you better explain what "but it still isn't working" means?. Does it not compile. Do you get an error? Does it copy the code to the wrong workbook?
 
Upvote 0
Ok yes, I set the reference you descripe above. By "not working" I mean that nothing changed from my original post. I can get the import if I open the new workbook (after the creation code is done running) and put the import code in the immediate window. However, when the import code is added into the creation code, the import never happens.
 
Upvote 0
Try this...
Code:
    [color=darkblue]For[/color] [color=darkblue]Each[/color] ws [color=darkblue]In[/color] wb.Worksheets
        [color=darkblue]If[/color] UCase(Left(ws.Name, 2)) = "CC" [color=darkblue]Then[/color]
            ws.Copy [color=green]'This copies the one worksheet to a new workbook.[/color]
            Application.VBE.ActiveVBProject.VBComponents.Import "T:\Finance Mgmt\Reporting\MacroExport.bas"
            ActiveWorkbook.SaveAs Filename:="C:\Users\lmcginle\Desktop\" & ws.Name
            ActiveWorkbook.Close SaveChanges:=[color=darkblue]True[/color]
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]Next[/color]

    wb.Activate

    Application.DisplayAlerts = [color=darkblue]True[/color]
    Application.ScreenUpdating = [color=darkblue]True[/color]


Macro to Add a Macro to New Workbooks
http://www.mrexcel.com/articles/copy-vba-module.php
 
Last edited:
Upvote 0
That didn't work either. So, I tried this since the error is that an Object is needed. But even with the below I still get the Object error. I feel like this should be so easy and I'm just missing something really stupid.


Sub Import()

On Error GoTo ErrorHandler
Dim VBProj As Object
Dim VBComp As Object
Dim VBImp As String

Set VBProj = ActiveWorkbook.VBProject
Set VBComp = VBProject.VBComponents("module4")
Set VBImp = ("T:\Finance Mgmt\Reporting\MacroExport.bas")

Application.VBE.VBProj.VBComp.Import VBImp

ErrorHandler:
MsgBox Error.Description

End Sub
 
Upvote 0
This should work...

Code:
[color=darkblue]Dim[/color] VBProj [color=darkblue]As[/color] [color=darkblue]Object[/color]
[color=darkblue]Dim[/color] VBImp [color=darkblue]As[/color] [color=darkblue]String[/color]

[color=darkblue]Set[/color] VBProj = ActiveWorkbook.VBProject
VBImp = "T:\Finance Mgmt\Reporting\MacroExport.bas"

VBProj.VBComponents.Import VBImp
 
Upvote 0
It still saying Error 424 Object Required. I'm stumped, because clearly the object is defined. Any other thoughts?
 
Upvote 0
Did you replace this...
Application.VBE.VBProj.VBComp.Import VBImp

With this...
VBProj.VBComponents.Import VBImp


It worked for me. The only difference being the path and name of of the .bas file.
 
Upvote 0

Forum statistics

Threads
1,214,994
Messages
6,122,633
Members
449,092
Latest member
bsb1122

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