Solved

Copy 1 row from every worksheet into a new workbook

Posted on 2011-02-21
5
235 Views
Last Modified: 2012-05-11
I have several excel file is a directory named c:\myreport

I want to copy the range A3:V3

From the tab named "DATA"

To a new worksheet
0
Comment
Question by:cd_morris
5 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34943977
Try this:
Sub CopySheetFromFiles()
    Dim wbk As Workbook, wbkThis As Workbook, wks As Worksheet
    Dim strParentFolder As String, strSheetName As String, strFile As String
    
    ' change this to match sheet name you want to copy
    strSheetName = "DATA"
    
    Set wbkThis = Workbooks.Add
    
    strParentFolder = "c:\myreport"
    
    Application.ScreenUpdating = False

    strFile = Dir(strParentFolder & "\*.xls")
   Do Until strFile = ""
      Set wbk = Workbooks.Open(strParentFolder & "\" & strFile)
      Set wks = wbk.Sheets(strSheetName)
      wks.Range("A3:V3").Copy wbkThis.Sheets(1).cells(rows.count, "A").End(xlup).Offset(1)
      wbk.Close False
      strFile = Dir
   Loop
    Application.ScreenUpdating = True
End Sub

Open in new window

0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 34943983
Assuming that you want to do this with every Excel file in that directory...



Sub GrabData()

    Dim fso As Object, fld As Object, fil As Object
    Dim SourceWb As Workbook
    Dim SourceWs As Worksheet
    Dim DestWs As Worksheet
    Dim DestRow As Long
    Dim WbWasOpen As Boolean

    Workbooks.Add
    Set DestWs = ActiveSheet

    Application.ScreenUpdating = False

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fld = fso.GetFolder("c:\myreport")
    For Each fil In fld.Files
        If LCase(fil.Name) Like "*.xls*" Then
            On Error Resume Next
            Set SourceWb = Workbooks(fil.Name)
            If Err = 0 Then
                WbWasOpen = True
            Else
                WbWasOpen = False
                Set SourceWb = Workbooks.Open(fil.Path)
                Err.Clear
            End If
            On Error GoTo 0
            SourceWs = SourceWb.Worksheets("DATA")
            DestRow = DestRow + 1
            SourceWs.[a3:v3].Copy DestWs.Cells(DestRow, 1)
            If Not WbWasOpen Then SourceWb.Close False
        End If
    Next

    Application.ScreenUpdating = True
    MsgBox "Done"

End Sub

Open in new window

0
 
LVL 4

Author Comment

by:cd_morris
ID: 34946046
matthewspatrick Your code gives me arun time error 91 on            
SourceWs = SourceWb.Worksheets("DATA")

0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 34946166
Sorry, should be:


            Set SourceWs = SourceWb.Worksheets("DATA")

Open in new window

0
 
LVL 42

Expert Comment

by:dlmille
ID: 34946596
Just a thought - If the input worksheets are the same, and are being updated, or copied into your folder for compilation, you could also create a new workbook, open the source files and directly link to the area you want a copy of.  Then, you just have to open your workbook and update links to refresh the data.

Dave
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

679 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