VBA Copy data from multiple workbooks in a folder, from a specific cell range on a single sheet into a single consolidation sheet in another workbook

chappy

New Member
Joined
Jul 18, 2006
Messages
42
Office Version
  1. 365
Platform
  1. Windows
I have a single consolidation workbook called "Consolidation"
I have multiple source workbooks stored in the same folder (Path "W:\Sourcedata\Opps")
In each of these source workbooks there is data on a worksheet called "Key_metrics". The sheet name will not change
The data is stored in cell range B5:BE64. The source data cell range will not change

I would like to copy the data from the source data cell range in B5:BE64 on worksheet "Key_metrics" from each of the workbooks in the folder into a sheet named "Consol" in the "Consolidation" workbook. I would like to copy and paste values into the next blank row available in column B.

If anyone can assist me it would be very much appreciated.

Thanks in advance
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Give this a try

VBA Code:
Sub t()
Dim fPath As String, fName As String, wb As Workbook, sh As Worksheet
fPath = "W:\Sourcedata\Opps\"
sh = Workbooks("Consolidation.xlsm").Sheets("Consol")
fName = Dir(fPath & "*.xls*")
    Do While fName <> ""
        If fName <> ThisWorkbook.Name Then
            Set wb = Workbooks.Open(fPath & fName)
                On Error Resume Next
                    If Not Sheets("Key_metrics") Is Nothing Then
                        Sheets("Key_metrics").Range("B5:BE64").Copy sh.Cells(Rows.Count, 2).End(xlUp)(2)
                    End If
                On Error GoTo 0
                Err.Clear
                wb.Close False
        End If
        fName = Dir
    Loop
End Sub
 
Upvote 0
Give this a try

VBA Code:
Sub t()
Dim fPath As String, fName As String, wb As Workbook, sh As Worksheet
fPath = "W:\Sourcedata\Opps\"
sh = Workbooks("Consolidation.xlsm").Sheets("Consol")
fName = Dir(fPath & "*.xls*")
    Do While fName <> ""
        If fName <> ThisWorkbook.Name Then
            Set wb = Workbooks.Open(fPath & fName)
                On Error Resume Next
                    If Not Sheets("Key_metrics") Is Nothing Then
                        Sheets("Key_metrics").Range("B5:BE64").Copy sh.Cells(Rows.Count, 2).End(xlUp)(2)
                    End If
                On Error GoTo 0
                Err.Clear
                wb.Close False
        End If
        fName = Dir
    Loop
End Sub

Thanks JLGWhiz. When I step into the code and run through line by line a

"Run-time error '91': Object variable or With block variable no set" error is triggered

The message appears at the "sh = Workbooks("Consolidation.xlsm").Sheets("Consol")" line.

Any ideas? Thanks again for responding
 
Upvote 0
I forgot to use Set in fron of the sh, so the object variable is not setting. Should be
VBA Code:
Set sh = Workbooks("Consolidation.xlsm").Sheets("Consol")
 
Upvote 0
I forgot to use Set in fron of the sh, so the object variable is not setting. Should be
VBA Code:
Set sh = Workbooks("Consolidation.xlsm").Sheets("Consol")

With a small tweak to paste formats as well as values it is working perfectly now thank you.

VBA Code:
Sub Consol()
Dim fPath As String
Dim fName As String
Dim wb As Workbook
Dim sh As Worksheet

fPath = "W:\Sourcedata\Opps\" sh = Workbooks("Consolidation.xlsm").Sheets("Consol")
fName = Dir(fPath & "*.xls*")
Do While fName <> ""
If fName <> ThisWorkbook.Name Then
Set wb = Workbooks.Open(fPath & fName)
On Error Resume Next
If Not Sheets("Key_metrics") Is Nothing Then
Sheets("Key_metrics").Range("B5:BE64").Copy
sh.Cells(Rows.Count, 2).End(xlUp).PasteSpecial Paste:=xlPasteValuesAndNumberFormats, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End If
On Error GoTo 0
Err.Clear
wb.Close False
End If
fName = Dir
    Loop

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,071
Latest member
cdnMech

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