Q regarding copy range within code

auto.pilot

Well-known Member
Joined
Sep 27, 2007
Messages
734
Office Version
  1. 365
Platform
  1. Windows
I have the following, which simply copies the range B6:Q100 from several files in a folder, then pastes data into the next row in my active workbook. Instead of B6:Q100, I would like to copy the entire populated range from each Sheet1 in each workbook in the Source Folder. Not sure if it matters, but the new copy range is much larger, typically A1:BP9000, but could be wider or taller depending on the data set.

Thanks in advance for any and all replies.

jim


Code:
Option Explicit
Const SOURCE_FOLDER = "\\myserver\mycompany\Templates\TemplateOne"

Sub AggregateDataFromFiles()
    Dim fs As Object
    Dim objFolder As Object
    Dim objFolderName As String
    objFolderName = SOURCE_FOLDER
    Dim filePath As String
    Dim objFile As Object
 
    Dim targetWb As Workbook
    Dim lastrow As Long
    lastrow = [A65536].End(3).Row + 5
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set objFolder = fs.GetFolder(objFolderName)
    For Each objFile In objFolder.Files
        filePath = objFolderName & "\" & objFile.Name
        Set targetWb = GetObject(filePath)
        targetWb.Worksheets(1).Range("B6:Q100").Copy Destination:=ActiveSheet.Range("A" & lastrow)
        lastrow = [A65536].End(3).Row + 1
        targetWb.Close (False)
    Next
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try

Code:
targetWb.Worksheets(1).UsedRange.Copy Destination:=ActiveSheet.Range("A" & lastrow)
 
Upvote 0
Excellent - and much faster than simply picking a larger-than-necessary range.

Thank you both.

jim
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top