Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

copies a module from one workbook to another

Status
Not open for further replies.

cogivina

Programmer
Jun 3, 2003
36
US
Hi,
I'm having a problem of understanding how the .BAS file work and running a macro that I obtained from the Exceltip.com. What I try to do is to copy a module1 from Book2.xls to Book1.xls. I get a "Subscript out of range" when I try to run this macro from the Book1.xls.

sub Sub GetModule()

CopyModule Workbooks("Book1.xls"), "Module1", Workbooks("Book2.xls")

end sub

--------------------------------------------
Sub CopyModule(ByVal SourceWB As Workbook, ByVal strModuleName As String, ByVal TargetWB As Workbook)

'This macro is from the ExcelTip.
'copies a module from one workbook to another.

Dim strFolder As String, strTempFile As String

strFolder = SourceWB.Path

If Len(strFolder) = 0 Then strFolder = CurDir
strFolder = strFolder & "\"
strTempFile = strFolder & "Module1.bas"

On Error Resume Next

SourceWB.VBProject.VBComponents(strModuleName).Export strTempFile
TargetWB.VBProject.VBComponents.Import strTempFile

On Error GoTo 0

End Sub
-------------------------

The code for Module1.bas is

Attribute VB_Name = "CheckWorkbookUpdates"

Sub Add_Macro()
Attribute Add_Macro.VB_Description = "copies a module from one workbook to another"
'
Range("A60") = "TESTTESTSTSTETS"

End Sub
-------------------------

Thanks.

 
I received the error because I did not have Workbooks2 opened.

My next question is how the variable strTempFile get used in the code, and why do I need to create this var (Module1.bas).

strTempFile = strFolder & "Module1.bas"

SourceWB.VBProject.VBComponents(strModuleName).Export strTempFile
TargetWB.VBProject.VBComponents.Import strTempFile

Thanks,
 
You can find some useful stuff in thread707-614467

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top