Automatically transfer data from multiple workbooks into a master workbook

Porkchopping

New Member
Joined
May 28, 2014
Messages
12
I am trying to automatically transfer data from nonadjacent cells (C1, B5,B10,B16,B22,B28) from multiple workbooks in a masterworkbook folder from A2:F2. I am a novice at VBA. I am not able to copy as Range("C1,B5,B10,B16,B22,B28") and the way it currently is coded only the last copied range (B28) is pasted to the master workbook. The data pastes to A2 in the master workbook instead of F2 where I want it. I need help copying the cells from the workbooks into row 2 in the master bookbook. Thanks in advance. Here is what I currently have:
Sub LoopThroughDirectory()
Dim MyFile As String
Dim erow
MyFile = Dir("C:\ToolFolder\WorkObjectives\")
Do While Len(MyFile) > 0
If MyFile = "zmaster.xlsm" Then
Exit Sub
End If
Workbooks.Open (MyFile)
Range("C1").Copy
Range("B5").Copy
Range("B10").Copy
Range("B16").Copy
Range("B22").Copy
Range("B28").Copy
ActiveWorkbook.Close
erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("sheet1").Range(Cells(erow, 1), Cells(erow, 6))
MyFile = Dir
Loop
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi welcome to the board.
Not able to test but see if update to your code helps:

Code:
Sub LoopThroughDirectory()
    Dim MyFile As String
    Dim wsMaster As Worksheet
    Dim wbSource As Workbook
    Dim erow As Long
    Dim data(6) As Variant
    Dim i As Integer
    Dim Item As Range

    'Your Master Sheet
    'CHANGE SHEET INDEX /NAME AS REQUIRED
    Set wsMaster = ThisWorkbook.Worksheets(1)

    MyFile = Dir("C:\ToolFolder\WorkObjectives\")
    Application.ScreenUpdating = False
    Do While Len(MyFile) > 0
        If MyFile = "zmaster.xlsm" Then Exit Sub
        Set wbSource = Workbooks.Open(MyFile)

        'Source Copy Sheet
        'CHANGE SHEET INDEX /NAME AS REQUIRED
        With wbSource.Sheets(1)
            For Each Item In .Range("C1,B5,B10,B16,B22,B28")
                data(i) = Item.Value
                i = i + 1
            Next
        End With
        erow = wsMaster.Cells(wsMaster.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
        wsMaster.Range("A" & erow & ":F" & erow).Value = data
        MyFile = Dir
        wbSource.Close False
        Set wbSource = Nothing
    Loop
    Application.ScreenUpdating = True
End Sub

Dave
 
Upvote 0
Since you are looping through more than one workbook, it is assumned you would want to list any subsequent iterations of the copied data to the next row.
Code:
Sub LoopThroughDirectory()
Dim MyFile As String, rng As Range, wb As Workbook
MyFile = Dir("C:\ToolFolder\WorkObjectives\*.xl*")
    Do While Len(MyFile) > 0
        If MyFile = "zmaster.xlsm" Then
            Exit Sub
        End If
        Set wb = Workbooks.Open(MyFile)
            With wb.Sheets(1)
                Set rng = Union(.Range("C1"), .Range("B5"), .Range("B10"), .Range("B16"), .Range("B22"), .Range("B28"))
                rng.Copy Workbooks("zmaster.xlsm").Sheets(1).Cells(Rows.Count, 2).End(xlUp)(2)
            End With
        wb.Close
        MyFile = Dir
    Loop
End Sub
 
Upvote 0
Hi Dave,
Thanks for the welcome and the assistance.

I am getting a Run-time error '9': Subscript out of range. This comes at the coding line: data (i) = Item.Value

Ryan
 
Upvote 0
Hi JLGWhiz,

Thanks for replying.

You are right that I want to list subsequent iterations of the copied data in the next row.

I received a Run-time error 1004: That command cannot be used on multiple sections. It appeared at:
rng.Copy Workbooks("zmaster.xlsm").Sheets(1).Cells(Rows.Count, 2).End(xlUp)(2)

I had something similar occur when I placed all the ranges together in one line of coding.
 
Upvote 0
Hi Dave,
Thanks for the welcome and the assistance.

I am getting a Run-time error '9': Subscript out of range. This comes at the coding line: data (i) = Item.Value

Ryan


Hi,
try setting variable i to 1 before the For Next loop as follows:

Rich (BB code):
    i = 1
    For Each Item In Range("C1,B5,B10,B16,B22,B28")
        data(i) = Item.Value
        i = i + 1
    Next

Dave
 
Upvote 0
Hi Dave,
There are no error messages anymore, and all the files in the folder open and close, but the only data that appears on the mastersheet is from the second file. It appears across row 2, under the column headers I created in row 1. All the data fills in one column to the right as well, starting at column B rather than column A.
Thanks again for your assistance.
Ryan
 
Upvote 0
Hi,
can't do much at moment but paste back code you are using & will have a look when can

Dave.
 
Upvote 0
I need to see a copy of the code with the adjustments you have made.
 
Upvote 0

Forum statistics

Threads
1,214,801
Messages
6,121,644
Members
449,045
Latest member
Marcus05

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