|
|
|
|
|
|
Excel
|
Copy Updated Sheet From Closed Workbook
|
|
Ease of Use
|
Easy
|
Version tested with
|
2003
|
Submitted by:
|
lucas
|
Description:
|
Deletes the old Sales sheet in the main workbook and copies the updated Sales sheet from the closed invoice workbook.
|
Discussion:
|
I found myself frequently needing to be sure I had the latest version of a particular sheet copied from a different, closed workbook. The Sales sheet should exist in both workbooks.
|
Code:
|
instructions for use
|
In a standard module:
Option Explicit
Sub ImportSalesSheet()
Dim Wkb As Workbook, BookKeep As Workbook
Dim strPath As String
Dim FileName As String
Dim strFullName As String
Dim IsOpen As Boolean
Dim i As Long
Application.EnableEvents = False
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Const SheetToCopy = "Sales"
strPath = ThisWorkbook.path
FileName = "Invoice.xls"
Set BookKeep = ActiveWorkbook
On Error GoTo NotFound
i = Sheets(SheetToCopy).Index
Sheets(i).Delete
NotFound:
If i = 0 Then i = 1
strFullName = strPath & "\" & FileName
If IsWbOpen(FileName) Then
Set Wkb = Workbooks(FileName)
IsOpen = True
Else
Set Wkb = Workbooks.Open(strFullName)
IsOpen = False
End If
Wkb.Sheets(SheetToCopy).Copy Before:=BookKeep.Sheets(i)
If Not IsOpen Then Wkb.Close False
Application.ScreenUpdating = True
MsgBox "Latest version of Sales Sheet successfully copied to this workbook.", vbInformation
Application.EnableEvents = True
Application.DisplayAlerts = True
Set BookKeep = Nothing
Set Wkb = Nothing
End Sub
Function IsWbOpen(wbName As String) As Boolean
On Error Resume Next
IsWbOpen = Len(Workbooks(wbName).Name)
End Function
|
How to use:
|
- Open the Visual Basic Editor by going to tools-Macro's-Visual Basic Editor or use Alt-F11
- On the toolbar of the Visual Basic Editor, go to insert - module
- In the module pane paste the code above.
- Close the Visual Basic Editor by clicking the X in the upper right corner or go to File-Close
|
Test the code:
|
- On the main menu go to tools-macro-macros.
- In the dialog window select ImportSalesSheet and then click run.
- The Sales sheet will be replaced with the one in the invoice.xls
-
- Make changes to the invoice.xls then close it and open the "run this file.xls" and run the macro to see the changes.
|
Sample File:
|
Copy updated sheet from closed workbook.zip 23.34KB
|
Approved by mdmackillop
|
This entry has been viewed 415 times.
|
|