Solved

Copy 1 row from every worksheet into a new workbook

Posted on 2011-02-21
5
236 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
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…

734 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