+ Reply to Thread
Results 1 to 6 of 6

Copying the Same Range from Multiple Workbooks

  1. #1
    Registered User
    Join Date
    02-06-2010
    Location
    Syracuse, NY
    MS-Off Ver
    Excel 2007
    Posts
    23

    Copying the Same Range from Multiple Workbooks

    I have a series of about 30 workbooks that each contain the same number of worksheets. All of the worksheets share the same name across all of the workbooks and the ranges within each of the worksheets are formatted the same.

    I'm looking for VBA code that will allow me to copy the same rows from the same worksheet across all workbooks and paste those rows in subsequent fashion in one workbook.

    For instance, say the 30 workbooks all contain one worksheet called "Customer Purchases". I'd like to copy rows 1 and 35 to 44 from each "Customer Purchases" from each of the 30 workbooks and paste these rows into one destination workbook with 30 tabs with each tab representing a respective workbook.

    In other words, "Sheet1" in the destination workbook would contain rows 1 and 35 to 44 from the "Customer Purchases" worksheet in workbook 1, "Sheet2" in the destination workbook would contain rows 1 and 35 to 44 from the "Customer Purchases" worksheet in workbook 2, and so on.

    Does anyone know how I can do this?

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Copying the Same Range from Multiple Workbooks

    Hello Anthony,

    Will the copied data overwrite what has been previously copied or appended to it?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    02-06-2010
    Location
    Syracuse, NY
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Copying the Same Range from Multiple Workbooks

    Quote Originally Posted by Leith Ross View Post
    Hello Anthony,

    Will the copied data overwrite what has been previously copied or appended to it?
    Leith,

    To answer your question: the data will be copied from each worksheet from the source workbooks to corresponding worksheets in one destination workbook.

    The source workbooks are segregated by year and the worksheets within each of the workbooks are similar in format. For example, the "Customer Purchases" worksheets are all similar in format in every one of the 30 workbooks, which again, are denominated by year.

    I'd like to get just a handful of rows from each yearly "Customer Purchases" worksheet and paste the values in one destination workbook with 30 tabs, with each tab corresponding to a different year and containing that year's Customer Purchases.

    The 30 source workbooks are all contained within a single directory.

    Regards,
    Tony


    Regards,
    Tony

  4. #4
    Registered User
    Join Date
    02-06-2010
    Location
    Syracuse, NY
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Copying the Same Range from Multiple Workbooks

    For those who are interested, I have poked around the internet and scoured my VBA books to find a solution to my particular problem. Here is what I found:

    Option Explicit

    Dim wbSourceBook As Workbook, wbDesBook As Workbook
    Dim wsSheet As Worksheet
    Dim rSourceRange As Range, rDesRange As Range
    Dim rNum As Long, i As Long, a As Long, r As Long, z As Long, e As Long, n As Long, m As Long
    Dim f As String, Directory As String

    Sub ListFiles()

    Directory = "D:\YourDirectoryHere\"
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False

    With ActiveSheet
    .Cells.ClearContents
    .Name = "Master"
    End With

    r = 1
    Cells(r, 1) = "FileName"
    Cells(r, 2) = "Size"
    Cells(r, 3) = "Date/Time"
    f = Dir(Directory & "*.xls")

    Do While f <> ""
    r = r + 1
    Cells(r, 1) = f
    Cells(r, 2) = FileLen(Directory & f) & " kb"
    Cells(r, 3) = FileDateTime(Directory & f)

    f = Dir()
    Loop

    Call AdjustColumns

    End Sub

    Sub AdjustColumns()

    For Each wsSheet In Application.Worksheets
    wsSheet.UsedRange.Cells.Columns.AutoFit
    Next wsSheet
    End Sub

    Sub AddSheetsBasedOnDataYear()

    Call ListFiles

    z = Cells(Rows.Count, 1).End(xlUp).Row

    For e = 2 To z
    If ActiveSheet.Cells(e, 1) <> ActiveWorkbook.Name Then
    n = InStr(Sheets("Master").Cells(e, 1), "C") + 2
    m = Mid(Sheets("Master").Cells(e, 1), n, 4)
    Sheets.Add.Name = m
    ActiveSheet.Move After:=Worksheets(Worksheets.Count)

    Set wbDesBook = ActiveWorkbook
    Set wbSourceBook = Workbooks.Open(Directory & Sheets("Master").Cells(e, 1))
    Set rSourceRange = wbSourceBook.Worksheets("SourceWb").Range("SourceRng")

    MsgBox wbDesBook.ActiveSheet.Name & " data will now be extracted"
    With rSourceRange
    Set rDesRange = wbDesBook.ActiveSheet.Cells(1, 1). _
    Resize(.Rows.Count, .Columns.Count)
    End With
    rDesRange.Value = rSourceRange.Value
    wbSourceBook.Close SaveChanges:=False
    End If
    Next e

    Call AdjustColumns

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

    End Sub
    Any comments on my code -- best practices, better efficiency, etc. -- will be appreciated.

    Tony

  5. #5
    Registered User
    Join Date
    01-12-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Copying the Same Range from Multiple Workbooks

    Hi,

    I realise this is an old post, but this is similar to my problem..

    I have got 77 subfolders within one main folder. Within each of these subfolders are x amount of excel files. I need to copy the range G6:G10 (from sheets named “Score Sheet”) from each of the excel files to one excel sheet in a new workbook.

    The range of G6:G10 in each of the excel files (within the subfolders) will be pasted to E2:H2, E3:H3, etc. in the new workbook.

    I would appreciate it if you could help!

  6. #6
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copying the Same Range from Multiple Workbooks

    Tian1,

    Unfortunately you need to post your question in a new thread, it's against the forum rules to post a question in the thread of another user. If you create your own thread, any advice will be tailored to your situation so you should include a description of what you've done and are trying to do. Also, if you feel that this thread is particularly relevant to what you are trying to do, you can surely include a link to it in your new thread.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ 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