Here's one I did a while back:
I called the following from my code (application.run("CopyOneModule"). In this case I'm taking module 3 and putting it in the new files. From what I understand, you have to export the module to text and import the module in your new workbook.
Private Sub CopyOneModule()
Dim FName2 As String
Dim FName3 As String
With Workbooks("Tool.xls")'name your workbook
FName2 = .path & "code.txt"
If FName2<> "" Then
On Error Resume Next
Kill FName2
End If
.VBProject.VBComponents("Module3").Export FName2
End With
FName3 = "Target Workbook Name.xls"
Workbooks(FName3).VBProject.VBComponents.Import FName2
Kill FName2
End Sub
And in module 3, I put the following procedure. All contents of module 3 should be in your target workbook.
Option Explicit
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Sub auto_open()
'Macro hand-crafted by Nate Oliver on 1/29/2002
Dim sUser As String
Dim lpBuff As String * 1024
GetUserName lpBuff, Len(lpBuff)
sUser = Left$(lpBuff, (InStr(1, lpBuff, vbNullChar)) - 1)
lpBuff = ""
On Error Resume Next
If ActiveWorkbook.Name = "Tool.xls" Then
Else:
If sUser<> "" Then
MsgBox prompt:="Welcome " & WorksheetFunction.Proper(sUser) & " to the new tool.", _
Title:="Welcome!"
Else: MsgBox prompt:="Welcome to the new tool", _
Title:="Welcome!"
End If
End If
End Sub
Modifying code gets tricky. But, check out the following, Chip does a nice job.
http://www.cpearson.com/excel/vbe.htm
Hope this helps. Cheers,
Nate
This message was edited by NateO on 2002-02-28 08:26