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 'Set sheetname to be copied Const SheetToCopy = "Sales" 'look for the Invoice workbook in this documents path strPath = ThisWorkbook.path 'comment line above and uncomment line below to use hard coded path for invoice.xls 'strPath = "C:\Documents\Test\" 'name of the file you wish to copy the Sales sheet from, change as needed FileName = "Invoice.xls" Set BookKeep = ActiveWorkbook 'Locate and delete the Sales Sheet from this workbook On Error GoTo NotFound i = Sheets(SheetToCopy).Index Sheets(i).Delete NotFound: 'Set value for i if sheet previously deleted 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 'Following line adds Sales sheet as the last sheet in the workbook 'Wkb.Sheets("Sales").Copy After:=BookKeep.Sheets(ThisWorkbook.Sheets.Count) 'comment line above and uncomment line below to copy sheet to same location 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 'Zack contributed functions to check if workbook and sheet is open and/or exists Function IsWbOpen(wbName As String) As Boolean On Error Resume Next IsWbOpen = Len(Workbooks(wbName).Name) End Function

How to use:

  1. Open the Visual Basic Editor by going to tools-Macro's-Visual Basic Editor or use Alt-F11
  2. On the toolbar of the Visual Basic Editor, go to insert - module
  3. In the module pane paste the code above.
  4. Close the Visual Basic Editor by clicking the X in the upper right corner or go to File-Close
 

Test the code:

  1. On the main menu go to tools-macro-macros.
  2. In the dialog window select ImportSalesSheet and then click run.
  3. The Sales sheet will be replaced with the one in the invoice.xls
  4. 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.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express