Solved

Copy 1 row from every worksheet into a new workbook

Posted on 2011-02-21
5
234 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

856 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