Results 1 to 4 of 4

Thread: Copy Selected Range From One Sheet To Multiple Workbooks Sheets Using VBA

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Copy Selected Range From One Sheet To Multiple Workbooks Sheets Using VBA

    I've been working with a project that I've asked a few questions about here already. This is part of the same project.

    I have a master spreadsheeet that has formulas in it to analyze data that is in different workbooks.

    The workbooks with the data on them are in the same folder.

    The spreadsheets with the data are named Book2 - Book174. xlsx.

    I wanted to either select a range, or identify a range, in a code, then have it copied and pasted into the exact same location in all the other spreadsheet files. Is there a code that can do this?

    For example, I have a column full of formulas, say in J:J, that I would like to copy to every one of the workbooks, Book2.xslx through Book174.xslx. I would like to paste all the formulas to the same column in every spreadsheet.

    Is this difficult? Can the code be made flexible so that if I copy a range then I could paste it a different range I determine, given they are the same size? Overwriting any existent data should not be a problem, because the ranges I would copy to should be blank. Thanks!

  2. #2
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Try this

    Code:
    Sub TCall()
    
        CopyAndPasteToMultipleWorkbooks "SourceSheetName", "J:J", "DestinationSheetName", "DestinationRangeAddress"
        
    End Sub
    
    
    Sub CopyAndPasteToMultipleWorkbooks(strFromSheet As String, strFromRange As String, strToSheet As String, strToRange As String)
         
        Dim strFile As String
        Dim strFileType As String
        Dim strPath As String
        Dim lngLoop As Long
        Dim wbk As Workbook
        
        strPath = "C:\ExcelFox"
        strFileType = "Book*.xlsx" 'Split with semi-colon if you want to specify the file types. Example ->> "*.xls;*.doc"
         
        For lngLoop = LBound(Split(strFileType, ";")) To UBound(Split(strFileType, ";"))
            strFile = Dir(strPath & "\" & Split(strFileType, ";")(lngLoop))
            Do While strFile <> ""
                If strFile <> ThisWorkbook.Name Then
                    Set wbk = Workbooks.Open(strPath & "\" & strFile, False, True)
                    With wbk.Sheets(strToSheet)
                        ThisWorkbook.Worksheets(strFromSheet).Range(strFromRange).Copy .Range(strToRange)
                        .Parent.Close 1
                    End With
                End If
            Loop
        Next lngLoop
         
        strFile = vbNullString
        strFileType = vbNullString
        strPath = vbNullString
        lngLoop = Empty
         
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0
    Nice! Ok, quick question, with this I can copy range J:J and it will paste straight into Column J of all the spreadsheets, right? Since I only see J:J in the code once, I suppose that as it's written this will copy to the same place where it was copied from - but in the close workbook...
    Last edited by Excel Fox; 05-14-2013 at 11:27 AM. Reason: Removed Quotes

  4. #4
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    If you wanted to copy a range from Sheet1!J:J of the original workbook, to Sheet2!M:M of all the other closed workbooks, you'd call the function by doing this.

    Code:
    Sub TCall()
    
        CopyAndPasteToMultipleWorkbooks "Sheet1", "J:J", "Sheet2", "M:M"
        
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

Similar Threads

  1. Replies: 4
    Last Post: 06-18-2013, 01:38 PM
  2. Replies: 2
    Last Post: 05-28-2013, 05:32 PM
  3. Combine Columns From Multiple Sheets To One Sheet
    By Portucale in forum Excel Help
    Replies: 6
    Last Post: 04-24-2013, 09:18 PM
  4. Replies: 2
    Last Post: 11-08-2012, 01:15 PM
  5. Copy selected data to other excel sheet
    By dhiraj.ch185 in forum Excel Help
    Replies: 2
    Last Post: 02-02-2012, 06:23 AM

Posting Permissions

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