Solved

Copy last row from multiple excel spread sheets into a summary spreadsheet

Posted on 2016-10-28
6
40 Views
Last Modified: 2016-10-30
Hello Experts,

I have a number of project excel spreadsheets, 1 per project, e.g. project1.xls, project2.xls, project3.xls, project4.xls to projectx.xls,

I want to create a project summary spreadsheet, e.g. projectsummary.xls, which needs to contain the last populated row of each of the individual spreadsheets - is this possible?

Many thanks

Jamie
0
Comment
Question by:Jamie
6 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 41864274
...is this possible?

Yes :)
0
 
LVL 33

Expert Comment

by:Norie
ID: 41864294
Where are the project files located?
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 41864320
This seems to be working for me.

Option Explicit

Sub Consolidate()
    
    Dim Paths As Variant
    Dim xPath As Variant
    Dim WbName As String
    Dim SourceWb As Workbook
    Dim SourceWs As Worksheet
    Dim DestWb As Workbook
    Dim DestWs As Worksheet
    Dim WbWasOpen As Boolean
    Dim Counter As Long
    Dim LastR As Long
    
    Paths = Application.GetOpenFilename("Excel files, *.xls*", , "Select project files", , True)
    
    If IsArray(Paths) = False Then
        MsgBox "You didn't select any files, aborting", vbCritical, "Invalid Entry"
        Exit Sub
    End If
    
    Set DestWb = Workbooks.Add
    Set DestWs = DestWb.Worksheets(1)
    
    For Each xPath In Paths
        WbName = Mid(xPath, InStrRev(xPath, "\") + 1)
        On Error Resume Next
        Set SourceWb = Workbooks(WbName)
        If Err = 0 Then
            WbWasOpen = True
        Else
            WbWasOpen = False
            Set SourceWb = Workbooks.Open(xPath)
        End If
        On Error GoTo 0
        Set SourceWs = SourceWb.Worksheets(1)
        With SourceWs
            If Counter = 0 Then
                .Rows(1).Copy DestWs.[a1]
                Counter = 1
            End If
            LastR = .Cells(.Rows.Count, 1).End(xlUp).Row
            If LastR > 1 Then
                Counter = Counter + 1
                .Rows(LastR).Copy DestWs.Cells(Counter, 1)
            End If
        End With
        If Not WbWasOpen Then SourceWb.Close False
    Next
    
    DestWs.Columns.AutoFit
    
    MsgBox "Done"
    
End Sub

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Jamie
ID: 41864930
Hi Patrick,

Apologies for delay in getting back to you, you came back amazingly fast with your great script - was not expecting an answer so quick!

Your script is 99% there, although the problem may be down to my test data files?

My test data is;

Project1. xls
A                  B
Update1-1      01/01/2016
Update1-2      01/02/2016
Update1-3      01/03/2016

Project2. xls
A                  B
Update2-1      02/01/2016
Update2-2      02/02/2016
Update2-3      02/03/2016

Project3. xls
A                  B
Update3-1      03/01/2016
Update3-2      03/02/2016
Update3-3      03/03/2016

When I run your script, the following is entered into;

ProjectSummary.xls
A                  B
Update1-1      01/01/2016            this row was not expected?
Update1-3      01/03/2016            correct
Update2-3      02/03/2016            correct
Update3-3      03/03/2016            correct

Any thoughts on why the 1st row is appearing?

Also, apologies, but thinking about it, rather than selecting the files, is it possible for ProjectSummary.xls to contain lookup data, which is automatically refreshed when the spreadsheet it loaded? e.g;

ProjectSummary.xls
A                              B                                          C
Project name 1            fullpathname/Project1. xls            Last row from Project1. xls      
Project name 2            fullpathname/Project1. xls            Last row from Project2. xls
Project name 3            fullpathname/Project1. xls            Last row from Project3. xls
,,,
Project name x            fullpathname/Projectx. xls            Last row from Projectx. xls


Many thanks

Regards

Jamie
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 41865584
NO POINTS FOR THIS.

Patrick provided for a header row above the data which is why you are getting the unexpected row.

Delete rows 39 to 42 to get what you want.
1
 

Author Closing Comment

by:Jamie
ID: 41865676
Hi Patrick,

Fantastic script, exactly what I asked for, very much appreciated. Saqib Husain, Syed - many thanks for the suggested ameneded also :)

Best regards

Jamie
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

747 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

11 Experts available now in Live!

Get 1:1 Help Now