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>

 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Try the following:


Code:
Option Explicit


Const FOLDER_PATH = "C:\Users\Dashboards (LIVE SOURCE)[B][COLOR=#0000ff]\[/COLOR][/B]"    'Must have the Backslash


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
    '
[COLOR=#0000ff]    Dim wbTarget As Workbook[/COLOR]
    
[COLOR=#0000ff]    Set wbTarget = ThisWorkbook[/COLOR]
    
    '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
        wbTarget.Sheets.Add after:=wbTarget.Sheets(wbTarget.Sheets.Count)
        Set wsTarget = wbTarget.ActiveSheet
        wsTarget.Range("D2:Z62").Value = wsSource.Range("D2:Z62").Value
[COLOR=#0000ff]        wsTarget.Name = Replace(sFile, ".xlsx", "")[/COLOR]
        
        '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
Thank you for helping with this. It is working to an extent but still not copying the data. This is possibly because the data in the range is an image (it is a dashboard comprised of linked images from another worksheet).

Is this an incorrect expression to copy across an image contained in the range D2:Z62?


Code:
wsTarget.Range("D2:Z62").Value = wsSource.Range("D2:Z62").Value

Thank you,

Jess
 
Upvote 0
Replace this line:

Code:
wsTarget.Range("D2:Z62").Value = wsSource.Range("D2:Z62").Value


By:

Code:
wsSource.Range("D2:Z62").Copy wsTarget.Range("D2")
 
Upvote 0
Question Copy from one workbook to another? I have a macro but is not working.. Here is my code. I want the macro to search for text "Total LOE" that is on a closed workbook and copy the row to a workbook that is active. Please advise

sub CopyLOE()
Dim ws As Worksheet
Dim DataFile As String

Set ws = ActiveSheet
MsgBox ("Please select a file to copy data from.")
DataFile = Application.GetOpenFilename("Excel Files(*.xls; *.xlsx; *.xlsm), *.xls; *.xlsx; *.xlsm", 1, "Select One File To Open", , False)
If DataFile <> "False" Then
Workbooks.Open DataFile, UpdateLinks:=False
Range("A41:N41").Copy
ws.Range("A85").PasteSpecial xlPasteValues
ActiveWorkbook.Close False
End If
End Sub
 
Upvote 0
Thank you again Dante,

This is now working almost perfectly. The only problem is that the copy/paste is distorting the x/y dimensions of the image. Outside of VBA, a copy/paste special PICTURE (U) maintains the correct proportions. Can the code be modified to a paste special type U?

Code:
wsSource.Range("D2:Z62").Copy wsTarget.Range("D2")

to

Code:
wsSource.Range("D2:Z62").Copy wsTarget.Range("D2").PasteSpecial Picture
or something similar?[TABLE="width: 1600"]
<tbody>[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Thank you once again for your guidance,

Jess[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited by a moderator:
Upvote 0
Hi Dante, thank you again for helping with this. Your modification works superbly; the new worksheets are being created and labelled plus the data is being copied across. I'm just left with two problems. The first is that the copy/paste is changing the x/y proportions of the images - can I prevent this with a Paste Special (picture U) somewhere in the code? The second is that I need to switch off the grid in the new worksheets.

I appreciate that you have already helped immensely, please would you advise on these final two points?

Kind regards,

Jess
 
Upvote 0
Try Option A:

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
        wbTarget.Sheets.Add after:=wbTarget.Sheets(wbTarget.Sheets.Count)
        Set wsTarget = wbTarget.ActiveSheet
        'wsTarget.Select
        wbTarget.Activate
        ActiveWindow.DisplayGridlines = False
        'wsTarget.Range("D2:Z62").Value = wsSource.Range("D2:Z62").Value
        wsSource.Cells.Copy wsTarget.Range("A1")
        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


Try option B:

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.Copy after:=wbTarget.Sheets(wbTarget.Sheets.Count)
        Set wsTarget = wbTarget.ActiveSheet
        'wsTarget.Select
        wbTarget.Activate
        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
Once again thank you for your reply. Both options work but distortion of x/y proportions is still a problem. Option B is better in that some proportions are preserved whilst others are not. Is there a way of converting the copy into a copy/paste special type picture U as this works outside of vba?

Best wishes,

Jess
 
Upvote 0
Activate the macro recorder, perform the steps and put here the resulting code of your macro.
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,733
Members
452,939
Latest member
WCrawford

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