Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50123

Macro to Append Data to Separate Workbook

$
0
0
Preface:
- I have a summary file where I house monthly data, tited: "YTD Summary.xlsm".
- One tab within this file simply reports the data, while the other tab ('DataDump') stores it in its raw form.
- In an Access database, I output a query to Excel, saving it in a folder specific to the month for which the data is being ran. Each month is saved separately as '01 Report_Summary.xlsx', '04 Report_Summary.xlsx', etc.

Within the 'DataDump' tab in the "YTD Summary.xlsm" file, I would like to have a Macro copy and paste the data from a separate Excel file for the month specified (the month in 'MM' format is entered in cell L1 in the 'DataDump' tab), and append that copied data to the dump tab - pasting it beginning at the next empty row.

The code below is what I have so far:

Code:

Sub Button1_Click()

    ChDir "C:\Users\User1\Desktop\2013 Reports\" & Range("L1") & " Report"
    Workbooks.Open Filename:= _
        "C:\Users\User1\Desktop\2013 Reports\" & Range("L1") & " Report" & "\" & Range("L1") & " Report_Summary.xlsx"
    Range("A2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Copy

    Windows("YTD Summary.xlsm").Activate
   
    FinalRow = Range("A65536").End(xlUp).Row
    For X = 1 To FinalRow
        ThisValue = Range("A" & X).Value
        If ThisValue <> "" Then
        NextRow = Range("A65536").End(xlUp).Row + 1
        Range("A" & NextRow).Select
            ActiveSheet.Paste
        End If
    Next X
   
End Sub

There is no issue gathering the data from the monthly files, as the macro currently copies the correct data from the correct file, but after the initial execution of the macro, running it again results in a pasting loop until the file errors out.

For example, if I enter "01" in cell L1 in the 'DataDump' tab, the macro will copy & paste the data from '2013 Reports\01 Report\01 Report_Summary.xlsx' no problem. However, if I am to enter "02" and run it again, the macro will copy & paste the (correct) data over and over until it errors.

Ideally, this Macro copy and pastes the data from the newest month, appending it to the next row to go in-line with the previous month's data. I'm terrible with loops though, and I'm not even sure if it's necessary to accomplish what I'm looking for, but hopefully this is enough for someone to see what I'm going for and provide some direction.

----Extra Credit: What is the code to close those monthly files that data is being extracted from? They are opened for the sake of copying, but how can I close them after the Macro executes?

Viewing all articles
Browse latest Browse all 50123

Trending Articles