Importing a .bas file into an excel to run the code in another one

AminShailja

New Member
Joined
Dec 2, 2019
Messages
11
Office Version
  1. 2010
Platform
  1. Windows
  2. Web
Hello,
I am trying to import a .bas file into an Excel1. but it directly saves as a Class module, while I need it to be saved as a module or in as a Excel object.
Also when I try to run the module, it gives error.

Please help me with this.

Below is the code I am trying for it.

VBA Code:
Sub importfile1()

Dim VBProj As Object 'VBIDE.VBProject
Dim myFileName As String

Workbooks.Open ("File1")
myFileName = "importfile.bas"
'MsgBox (ActiveWorkbook.Name)
Set VBProj = Nothing
On Error Resume Next
Set VBProj = ActiveWorkbook.VBProject
On Error GoTo 0

If VBProj Is Nothing Then
MsgBox "Can't continue--I'm not trusted!"
Exit Sub
End If

VBProj.vbcomponents.Import myFileName

Application.Run ("'file2'!sheet11.Macro1")

End Sub
 
Last edited by a moderator:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Your code works correctly and inserts the module as module into the File1.xlsx (or File1.xlsm - whichever you have in the same folder with this workbook) file.

However, I am not sure about the Application.Run ("'file2'!sheet11.Macro1").

If you are trying to run a macro called Macro1 in the imported module (imporfile.bas), then you should use:

VBA Code:
Application.Run ("'file1'!Macro1")

Yes, file name should also match - File1.
 
Upvote 0
No, for me it directly opens a class module and imports it there.
Is it possible that I would need to do any system changes to import the file as a module?
 
Upvote 0

Forum statistics

Threads
1,214,621
Messages
6,120,563
Members
448,972
Latest member
Shantanu2024

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