Results 1 to 8 of 8

Loop to copy from multiple files and sheets to one long list

Threaded View

  1. #2
    Valued Forum Contributor
    Join Date
    12-05-2011
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010 & 2013
    Posts
    308

    Re: Loop to copy from multiple files and sheets to one long list

    Hi sdb

    Housekeeping thing first - please could you edit your post and surround your code with code tags (start with [ c o d e ] and end with [ / c o d e ] no spaces.)? Makes it easier to read, and is a forum rule. Thanks.

    You don't need to swap between workbooks. You can just refer to the workbook, worksheet and range from where you are. Unfortunately, the macro recorder doesn't really help in learning this!

    Also, set a variable as any workbook or worksheet you are going to use frequently - saves on typing!

    Anyway, here's a loop for you:
    Sub LoopThrough(strSheet As String)
    Dim wb As Workbook, ws As Worksheet, Wwb As Workbook
    Dim destn As Range
    Set Wwb = Workbooks("Weekly.xls")
    Set ws = Wwb.Sheets("Summary")
    
    For Each wb In Application.Workbooks
      If wb.Name <> "Weekly.xls" Then
        'Set the destination as the last cell in column K,
        'offset (1,0) to get the next one down.
        Set destn = ws.Range("K65000").End(xlUp).Offset(1, 0)
        wb.Sheets(strSheet).Range("A4:P43").Copy Destination:=destn
    
        'Reset destn
        Set destn = ws.Range("K65000").End(xlUp).Offset(1, 0)
        wb.Sheets(strSheet).Range("R4:AG43").Copy Destination:=destn
        End If
    
      Next wb
    
    End Sub
    Put this in a module in your workbook. For the Checkbox click code, just send the sheet to the above code:
    Private Sub CheckBox10_Click()
    Call LoopThrough("10")
    End Sub
    I'm assuming it will be the same sheet in every workbook, e.g. "1" or "10", etc.

    Cheers, Rob.
    Last edited by rscsmith; 03-04-2012 at 12:18 AM.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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