Solved

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

Posted on 2014-10-05
6
1,972 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
This article will shed light on the latest trends when it comes to your resume building needs. For far too long, the traditional CV format has monopolized the recruitment market.
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

758 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

19 Experts available now in Live!

Get 1:1 Help Now