Sub UpdateUserBook()
Filename = "UserBook.xls"
' Activate workboook
On Error Resume Next
Workbooks(Filename).Activate
If Err <> 0 Then
MsgBox Filename & " must be open!", vbCritical
Exit Sub
End If
Msg = "This macro will replace Module1 in UserBook.XLS "
Msg = Msg & "with an updated Module." & vbCrLf & vbCrLf
Msg = Msg & "Click OK to continue."
If MsgBox(Msg, vbInformation + vbOKCancel) = vbOK Then
Call ReplaceModule
Else
MsgBox "Module not replaced!", vbCritical
End If
End Sub
Sub ReplaceModule()
' Export Module1 from this workbook
Filename = ThisWorkbook.Path & "\tempmodxxx.bas"
ThisWorkbook.VBProject.VBComponents("Module1") _
.Export Filename
' Replace Module1 in UserBook
Set VBP = ActiveWorkbook.VBProject
On Error GoTo ErrHandle
With VBP.VBComponents
.Remove VBP.VBComponents("Module1")
.Import Filename
End With
' Delete the temorary module file
Kill Filename
MsgBox "The module has been replaced.", vbInformation
Exit Sub
ErrHandle:
' Did an error occur?
MsgBox "ERROR. The module may not have been replaced.", _
vbCritical
End Sub