Solved

Excel VBA Append Data From Closed Workbook to Active Workbook/worksheet

Posted on 2014-10-05
6
2,019 Views
Last Modified: 2014-10-07
I am trying to figure out a VBA macro to allow user to open a closed workbook, with the aid of FileOpen, select Sheet 1 which has data from range A7:G last row. Evaluate first row date to append the data to the active sheet in the first workbook without overwriting current data.
The opening of two workbooks and copy paste is taking too long considering there is one master and several workbooks to import from on a daily basis.
0
Comment
Question by:DougDodge
  • 2
  • 2
6 Comments
 
LVL 25

Expert Comment

by:ProfessorJimJam
ID: 40364266
Sub Workbook_test()
    Dim wb As Workbook
    Dim rng As Range
    Dim lastRow As Long
    ' turn off the screen updating
    Application.ScreenUpdating = False
    Set wb = Workbooks.Open("C:\FOLDERNAME\yourfile.xls", True, True)
    'range of data from the source workbook
    Set rng = wb.Worksheets("sheet1").Cells(.Rows.Count, "A").End(xlUp).Row + 1
    'the sheet in this workbook to copy to
    With ThisWorkbook.Worksheets("sheet1")
        'find last used row
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
        'copy range to it
        rng.Copy .Range("A" & lastRow)
    End With
    ' close the source workbook without saving any changes
    wb.Close False
    ' free memory
    Set wb = Nothing
    ' turn on the screen updating
    Application.ScreenUpdating = True
End Sub

Open in new window

0
 

Author Comment

by:DougDodge
ID: 40364854
Sorry, the code does not seem to work. It issues an "Invalid or unqualified Reference"

It hangs up here:

 Set rng = wb.Worksheets("sheet1").Cells(.Rows.Count, "A").End(xlUp).Row + 1
0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40365279
While I suspect your instructions are not complete in what you wish to do, this code will do the following:
* identify the next available cell in the active sheet in which to append data (based on column A)
* prompt the user to open an Excel workbook (presumably to import data from)
* copy the range in the first sheet of that workbook from A7:Glastrow
* paste that copied selection to the original workbook.
* close the import workbook.
Option Explicit
Sub Append_Data()
    Dim strDest As String
    Dim lngLastSourceRow As Long
    Dim strFilePath, strImportFileName As String
    Dim strDestFile, strDestSheet As String
    
    Application.ScreenUpdating = False
    strDestFile = ActiveWorkbook.Name
    strDestSheet = ActiveSheet.Name
    strDest = Range("A2").End(xlDown).Offset(1, 0).Address
    
    strFilePath = Application.GetOpenFilename("Excel Files (*.xls;*.xlsx), *.xls;*.xlsx", , "Open Excel File to Import")
    If strFilePath = "False" Then Exit Sub
    Workbooks.Open (strFilePath)
    strImportFileName = ActiveWorkbook.Name
    Sheets(1).Select 'assumes data to be copied is on the first sheet, regardless of name
    lngLastSourceRow = Range("A" & Cells.Rows.Count).End(xlUp).Row
    Sheets("Sheet1").Range("A7:G" & lngLastSourceRow).Copy
    Workbooks(strDestFile).Sheets(strDestSheet).Range(strDest).PasteSpecial
    Application.CutCopyMode = False
    Workbooks(strImportFileName).Close savechanges:=False
    
    Application.ScreenUpdating = True
End Sub

Open in new window


Regards,
-Glenn
0
 

Author Closing Comment

by:DougDodge
ID: 40366066
Thanks.... A couple of slight changes and it works perfectly.
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40366133
You're welcome; I'm glad I could help.

-Glenn
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now