Copy non adjacent data from multiple files into Master file

jamesplant77

New Member
Joined
Nov 13, 2015
Messages
4
Good morning.

I have VBA that allows me to aggregate data from multiple workbooks in a specified folder and then copy lots of non adjacent data from these workbooks to create a master data file.

It all works fine apart from it is only allowing me to copy a certain amount of cells, before the VBA returns a 1004 error.

This is the VBA:

ub copyNonAdjacentCellData()
Dim myFile As String, path As String
Dim NextRow As Long, col As Long

path = "C:\Users\JamesPlantCloud21\Desktop\Timesheets - STH Staff\"
'C:\Users\JamesPlantCloud21\Desktop\Timesheets - STH Staff
myFile = Dir(path & "*.xlsx")

Application.ScreenUpdating = False

Do While myFile <> ""
Workbooks.Open (path & myFile)
Windows(myFile).Activate

Set copyrange = Sheets("JAN 23").Range("C4,C5,C9,D9,E9,F9,G9,H9,I9,J9,K9,AQ9,AR9,AS9,AT9,C10,D10,E10,F10,G10,H10,I10,J10,K10,AQ10,AR10,AS10,AT10,C11,D11,E11,F11,G11,H11,I11,J11,K11,AQ11,AR11,AS11,AT11")

Windows("Timesheet_Aggregator_STH.xlsm").Activate

NextRow = ThisWorkbook.Sheets("STH_STAFF").Cells(Rows.Count, 1).End(xlUp).Row + 1


col = 1
For Each cell In copyrange
cell.Copy


Cells(NextRow, col).PasteSpecial xlPasteValues

col = col + 1

Next

Windows(myFile).Close SaveChanges:=False

myFile = Dir()

Loop

Range("A:CB").EntireColumn.AutoFit

Application.ScreenUpdating = True


End Sub

As you can see from the range, I am able to copy row 9,10 & 11 perfectly, but when I expand the range to include rows 12,13, & 14 I get the error. Is there a way that I could change to C9:K9,AQ9:AT9,C10:K10,AQ10,AT10 etc...
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi,
untested but see if this update to your code resolves the issue

VBA Code:
Sub copyNonAdjacentCellData()
    Dim myFile                   As String, path As String
    Dim CopyMonth           As String
    Dim NextRow               As Long, col As Long
    Dim wbCopy                 As Workbook
    Dim wbDest                  As Worksheet
    Dim arr()                       As Variant
  
    path = "C:\Users\JamesPlantCloud21\Desktop\Timesheets - STH Staff\"
  
    CopyMonth = "JAN 23"
  
  
    'C:\Users\JamesPlantCloud21\Desktop\Timesheets - STH Staff
    myFile = Dir(path & "*.xlsx")
  
    On Error GoTo myerror
  
    Set wsDest = ThisWorkbook.Worksheets("STH_STAFF")
  
    Application.ScreenUpdating = False
  
    Do While myFile <> ""
  
        Set wbCopy = Workbooks.Open(path & myFile)
      
        Set copyrange = wbCopy.Worksheets(CopyMonth).Range("C4,C5,C9,D9,E9,F9,G9,H9,I9,J9,K9,AQ9,AR9,AS9,AT9,C10," & _
                                                           "D10,E10,F10,G10,H10,I10,J10,K10,AQ10,AR10,AS10,AT10," & _
                                                            "C11,D11,E11,F11,G11,H11,I11,J11,K11,AQ11,AR11,AS11,AT11")
        ReDim arr(1 To copyrange.Cells.Count)
      
        For Each cell In copyrange
            col = col + 1
            arr(col) = cell.Value
        Next cell
      
        With wsDest
            NextRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
            'post array to range
            .Cells(NextRow, 1).Resize(, UBound(arr)).Value = arr
        End With
      
        wbCopy.Close False
      
        myFile = Dir()
      
        Set wbCopy = Nothing
        Set copyrange = Nothing
        col = 0
    Loop
  
  
myerror:
    If Not wbCopy Is Nothing Then wbCopy.Close False
    Application.ScreenUpdating = True
    If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,739
Members
448,989
Latest member
mariah3

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