Copy several cells form multiple workbooks into a new workbook.

migces

New Member
Joined
Nov 28, 2018
Messages
10
Hello,

I have many workbooks, each with only one worksheet, containing a wide range of data related to one candidate for a work position. I need to copy specific data from each candidate into a new workbook that will contain all the candidates so i can look in one place for all the relevent data for all candidates, rather than opening each individual candidate file.

For example:

I need to copy the cells D7,K10:K13,K16:K21,K24:K29,K34:K37 and K39, from each candidate workbook, into a row on the new unified workbook.

Hope it made sense.

Thx for any help.

Cheers
 
Try:
Code:
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim wkbDest As Workbook, wkbSource As Workbook, desWS As Worksheet
    Set desWS = ThisWorkbook.Sheets("Work Candidates")
    Const strPath As String = "C:\workfiles\"
    ChDir strPath
    strExtension = Dir(strPath & "*.xls*")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource
            ActiveSheet.Range("D7").Copy
            desWS.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
            ActiveSheet.Range("K10:K13,K16:K21,K24:K29,K34:K37,K39").Copy
            desWS.Cells(Rows.Count, "B").End(xlUp).Offset(1, 0).PasteSpecial Transpose:=True, Paste:=xlPasteValues
            .Close savechanges:=False
        End With
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Thanks. That works great but i have discovered a new problem:

I've tested with different number of files on the source folder and i found out that the D7 value is the right one for the first file but the next values from the K column are from the next file......

As an example:

Say i have 2 files: candidate_A.xls and candidate_B.xls

When i run the macro what happens is that both candidates names are pasted right, but the candidate A receives the data from the candidate B file and candidate B receives no data, just blanks.

Seems that it opens the candidate A file --> copies the D7 value from it into A2 cell --> closes the file --> opens the candidate B file --> copies the values from the K range into the B2,C2,D2,E2, etc... cells.


Hope it made some sence....

Thx again
 
Upvote 0
Are any of the D7 cells in any of the files blank?
 
Upvote 0
No, all the files have the D7 filled with the candidate name.

Here is a template of the source candidate file: https://ufile.io/06yeq

All the values are pasted correctly only if i have 1 file on the folder. As soon as i add another file, the above behaviour happens.
 
Upvote 0
No, all the files have the D7 filled with the candidate name.

Here is a template of the source candidate file: https://ufile.io/06yeq

All the values are pasted correctly only if i have 1 file on the folder. As soon as i add another file, the above behaviour happens.

Update:

A bit of testing and found out that by filling all the K cells with values, the macro works great. Problem is that most K cells are blank because there was no need to fill them per candidate evaluation.

Any way that if the cell is blank, just copy blank, or 0?

Thx.
 
Upvote 0
See if this works for you:
Code:
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim wkbDest As Workbook, wkbSource As Workbook, desWS As Worksheet, bottomA As Long
    Set desWS = ThisWorkbook.Sheets("Work Candidates")
    Const strPath As String = "C:\workfiles\"
    ChDir strPath
    strExtension = Dir(strPath & "*.xls*")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource
            ActiveSheet.Range("D7").Copy
            bottomA = desWS.Range("A" & desWS.Rows.Count).End(xlUp).Row + 1
            desWS.Cells(bottomA, 1).PasteSpecial xlPasteValues
            ActiveSheet.Range("K10:K13,K16:K21,K24:K29,K34:K37,K39").Copy
            desWS.Cells(bottomA, 2).PasteSpecial Transpose:=True, Paste:=xlPasteValues
            .Close savechanges:=False
        End With
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,726
Members
448,987
Latest member
marion_davis

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