+ Reply to Thread
Results 1 to 7 of 7

Copy a row from 3000 excel files stored in a folder and paste it in a new workbook

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Re: Copy a row from 3000 excel files stored in a folder and paste it in a new workboo

    The following should do as you require. Note that you will need to set the folder to look in appropriately.
    This code should be placed into a module in the VBE.

    Option Explicit
    
    Sub Get_the_rows()
    
    Const FOLDER_TO_LOOK_IN = "C:\My Excel Files"
    
    Dim Input_Path As String, Input_File As String
    Dim Output_Row As Long
    Dim input_wb As Workbook, input_ws As Worksheet
    Dim oldCalcMethod As Long
    
    If Right(FOLDER_TO_LOOK_IN, 1) <> "\" Then
        Input_Path = FOLDER_TO_LOOK_IN & "\"
    Else
        Input_Path = FOLDER_TO_LOOK_IN
    End If
    
    Application.ScreenUpdating = False
    oldCalcMethod = Application.Calculation
    Application.Calculation = xlCalculationManual
    
    With Worksheets(1)
        Output_Row = .UsedRange.Rows.Count + 1
        If Output_Row = 2 And WorksheetFunction.CountA(.Rows(1)) = 0 Then Output_Row = 1
        
        Input_File = Dir(Input_Path & "*.xls")
        Do While Input_File <> "" And Output_Row < .Rows.Count
            On Error Resume Next
            Set input_wb = Nothing
            Set input_wb = Workbooks.Open(Input_Path & Input_File)
            If Not input_wb Is Nothing Then
                Set input_ws = Nothing
                Set input_ws = input_wb.Worksheets("Sheet1")
                If Not input_ws Is Nothing Then
                    input_ws.Rows(2).Copy Destination:=.Rows(Output_Row)
                    Output_Row = Output_Row + 1
                End If
            End If
            input_wb.Close savechanges:=False
            Input_File = Dir()
            On Error GoTo 0
        Loop
    End With
    Application.ScreenUpdating = True
    Application.Calculation = oldCalcMethod
    MsgBox "Complete"
    End Sub
    If you find the response helpful please click the scales in the blue bar above and rate it
    If you don't like the response, don't bother with the scales, they are not for you

  2. #2
    Registered User
    Join Date
    07-27-2009
    Location
    Bratislava, Slovakia
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Copy a row from 3000 excel files stored in a folder and paste it in a new workboo

    Thank you!! Works like a charm!

    If it's not too much trouble for you, could you please explain the code a little bit? I'd very much like to learn some new VBA stuff.

    Thank you again!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1