Copying a range from multiple workbooks to summary workbooks with corresponding worksheets

jessrabbit

New Member
Joined
Feb 26, 2011
Messages
18
Hello, I've researched many similar examples but none that I've found meet my needs and my VBA skills aren't good enough to adapt. Please would someone kindly help. I have a folder containing workbooks which I want to copy a specific range of data from one named worksheet and then paste into a newly created worksheet in a summary workbook where the new worksheet is labelled with the source workbook name.

The source data folder contains a variable number of workbooks (probably between 5 and 25) so the new summary workbook would contain a variable number of worksheets.

The source workbook would be named eg. 201905TV21 (year 2019, week 05, manager TV, project 21) and this would become the label for the new worksheet into which the range of data would be copied. This is so that the origin of the data is confirmed in the summary workbook.

Could someone help me to get on the right track with this please?

I've made a start with the code below but;

1. it doesn't copy the data across.
2. I need it to paste the data into separate worksheets in the target workbook. That is;

source workbook 1 data copied to target workbook x worksheet 1
source workbook 2 data copied to target workbook x worksheet 2
source workbook 3 data copied to target workbook x worksheet 3 etc.

Thanks,

Jess

Here's the code I have now;

<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">Option Explicit


Const FOLDER_PATH = "C:\Users\Dashboards (LIVE SOURCE)"


Sub ImportWorksheets()
'=============================================
'Process all Excel files in specified folder
'=============================================
Dim sFile As String 'file to process
Dim wsTarget As Worksheet
Dim wbSource As Workbook
Dim wsSource As Worksheet
Dim rowTarget As Long 'output row

rowTarget = 2

'check the folder exists
If Not FileFolderExists(FOLDER_PATH) Then
MsgBox "Specified folder does not exist, exiting!"
Exit Sub
End If

'reset application settings in event of error
On Error GoTo errHandler
Application.ScreenUpdating = False

'set up the target worksheet
Set wsTarget = Sheets("Sheet2")

'loop through the Excel files in the folder
sFile = Dir(FOLDER_PATH & "*.xlsx*")
Do Until sFile = ""

'open the source file and set the source worksheet
Set wbSource = Workbooks.Open(FOLDER_PATH & sFile)
Set wsSource = wbSource.Worksheets("Dashboard")

'import the data
With wsTarget
.Range("D2:Z62").Value = wsSource.Range("D2:Z62").Value

End With

'close the source workbook, increment the output row and get the next file
wbSource.Close SaveChanges:=False
rowTarget = rowTarget + 1
sFile = Dir()
Loop

errHandler:
On Error Resume Next
Application.ScreenUpdating = True

'tidy up
Set wsSource = Nothing
Set wbSource = Nothing
Set wsTarget = Nothing
End Sub




Private Function FileFolderExists(strPath As String) As Boolean
If Not Dir(strPath, vbDirectory) = vbNullString Then FileFolderExists = True
End Function</code>

 
Hello, here it is;


Code:
Sub Macro2()
'
' Macro2 Macro
'


'
    Range("B2:Z62").Select
    Selection.Copy
    Sheets("Sheet2").Select
    Range("B1").Select
    ActiveSheet.Pictures.Paste.Select
    ActiveSheet.Shapes.Range(Array("Picture 1")).Select
    ActiveWindow.DisplayGridlines = False
End Sub

Thanks,

Jess
 
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Ok, try with this:

Code:
Option Explicit


Const FOLDER_PATH = "C:\Users\Dashboards (LIVE SOURCE)\"


Sub ImportWorksheets()
    '=============================================
    'Process all Excel files in specified folder
    '=============================================
    Dim sFile As String 'file to process
    Dim wsTarget As Worksheet
    Dim wbSource As Workbook
    Dim wsSource As Worksheet
    Dim rowTarget As Long 'output row
    '
    Dim wbTarget As Workbook
    
    Set wbTarget = ThisWorkbook
    
    'check the folder exists
    If Not FileFolderExists(FOLDER_PATH) Then
        MsgBox "Specified folder does not exist, exiting!"
        Exit Sub
    End If
    
    Application.ScreenUpdating = False
    
    'loop through the Excel files in the folder
    sFile = Dir(FOLDER_PATH & "*.xlsx*")
    Do Until sFile = ""
    
        'open the source file and set the source worksheet
        Set wbSource = Workbooks.Open(FOLDER_PATH & sFile)
        Set wsSource = wbSource.Worksheets("Dashboard")
        
        'import the data
        wsSource.Range("B2:Z62").Copy
        wbTarget.Sheets.Add after:=wbTarget.Sheets(wbTarget.Sheets.Count)
        Set wsTarget = wbTarget.ActiveSheet
        wbTarget.Activate
        wsTarget.Select
        ActiveSheet.Pictures.Paste.Select
        ActiveSheet.Shapes.Range(Array("Picture 1")).Select
        ActiveWindow.DisplayGridlines = False
        wsTarget.Name = Replace(sFile, ".xlsx", "")
        
        'close the source workbook, increment the output row and get the next file
        wbSource.Close SaveChanges:=False
        sFile = Dir()
    Loop
    
    Application.ScreenUpdating = True
    'tidy up
    Set wsSource = Nothing
    Set wbSource = Nothing
    Set wbTarget = Nothing
    Set wsTarget = Nothing
End Sub
'
Private Function FileFolderExists(strPath As String) As Boolean
    If Not Dir(strPath, vbDirectory) = vbNullString Then FileFolderExists = True
End Function
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,857
Members
449,051
Latest member
excelquestion515

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