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'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.