Solved

Copy 1 row from every worksheet into a new workbook

Posted on 2011-02-21
5
232 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry, should be:


            Set SourceWs = SourceWb.Worksheets("DATA")

Open in new window

0
 
LVL 41

Expert Comment

by:dlmille
Comment Utility
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

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.

Join & Write a Comment

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

728 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

13 Experts available now in Live!

Get 1:1 Help Now