Open file copy and paste when value changes

rholdren

Board Regular
Joined
Aug 25, 2016
Messages
140
Office Version
  1. 365
  2. 2019
Excel 365 - I have a worksheet that has a list of file names (entire path) 5 columns of data ( C-G) that follows the file name. I can have excel copy the data in columns C-G on copy one by one down the list but what I was looking for is a shortcut. Instead of having Excel copy the line of data in row two then open the file, paste, save and close the file. Then return to row 3 copy the data open the file location, copy paste save and close (what could be the exact same file - or not) and continue to repeat this process until it reaches the bottom of the list. In the example below Donald Duck has two rows, below him Daisy has eight rows. Rather than have each of the file opened and closed multiple time to to copy and paste ranges I was trying to find a shortcut to open each file once. I get a similar file on a monthly basis and I do it manually each month. Any Thoughts?

2242760C:\PersonalReviews\Donald Duck.xls202207119213844
2242760C:\PersonalReviews\Donald Duck.xls202208117213843
2240553C:\PersonalReviews\Daisy Duck.xls202201144101515
2240553C:\PersonalReviews\Daisy Duck.xls202202139101513
2240553C:\PersonalReviews\Daisy Duck.xls202203133111512
2240553C:\PersonalReviews\Daisy Duck.xls202204134111513
2240553C:\PersonalReviews\Daisy Duck.xls202205134121513
2240553C:\PersonalReviews\Daisy Duck.xls202206136111514
2240553C:\PersonalReviews\Daisy Duck.xls202207134111313
2240553C:\PersonalReviews\Daisy Duck.xls202208134121313
2139208C:\PersonalReviews\Mickey Mouse.xls20210858191637
2139208C:\PersonalReviews\Mickey Mouse.xls20210956181635
2139208C:\PersonalReviews\Mickey Mouse.xls20211060171534
2139208C:\PersonalReviews\Mickey Mouse.xls20211156161335
2139208C:\PersonalReviews\Mickey Mouse.xls20211258171136

Any help would be greatly appreciated.

Thanks :)
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
You will need to tweak it a little but something like this should do it.

VBA Code:
Sub OpenWorkbookInListAndPasteData()
    Dim srcWB As Workbook, destWB As Workbook
    Dim srcSht As Worksheet, destSht As Worksheet
    Dim oDict As Object
    Dim srcRng As Range, rngTemp As Range
    Dim iRow As Long
    Dim vKey As Variant
    Dim sKey As String
    
    Set srcWB = ThisWorkbook
    Set srcSht = ActiveSheet    ' <--- Leave as activesheet or specify sheet name
'    Set destWB = ThisWorkbook
'    Set destSht = destWB.Worksheets("PasteData")
    
    'create late bound dictionary object
    Set oDict = CreateObject("Scripting.Dictionary")
    oDict.comparemode = vbTextCompare
    
    'the data to add
    Set srcRng = srcSht.Cells(1, 1).CurrentRegion
    'skip header row
    For iRow = 2 To srcRng.Rows.Count
        
        'save the key value - which is the file name to open
        sKey = CStr(srcSht.Cells(iRow, 2).Value)
        
        'if the key is already in the dictionary, then Union the other row with the same key
        With srcRng
            If oDict.exists(sKey) Then
                Set rngTemp = oDict(sKey)
                Set rngTemp = Union(rngTemp, .Range(.Cells(iRow, 3), .Cells(iRow, 7)))
                Set oDict(sKey) = rngTemp
            'otherwise just add the key and row
            Else
                Set rngTemp = .Range(.Cells(iRow, 3), .Cells(iRow, 7))
                Set oDict(sKey) = rngTemp
            End If
        End With
    Next iRow
    
    'loop through the dictionary keys & paste
    For Each vKey In oDict.keys
        Set destWB = Workbooks.Open(vKey)
        Set destSht = destWB.Worksheets(1)          ' <--- Specify sheet to use in paste
        Set rngTemp = oDict(vKey)
        rngTemp.Copy Destination:=destSht.Range("A" & Rows.Count).End(xlUp).Offset(1)
    Next vKey
    Set oDict = Nothing
End Sub
 
Upvote 0
You will need to tweak it a little but something like this should do it.

VBA Code:
Sub OpenWorkbookInListAndPasteData()
    Dim srcWB As Workbook, destWB As Workbook
    Dim srcSht As Worksheet, destSht As Worksheet
    Dim oDict As Object
    Dim srcRng As Range, rngTemp As Range
    Dim iRow As Long
    Dim vKey As Variant
    Dim sKey As String
   
    Set srcWB = ThisWorkbook
    Set srcSht = ActiveSheet    ' <--- Leave as activesheet or specify sheet name
'    Set destWB = ThisWorkbook
'    Set destSht = destWB.Worksheets("PasteData")
   
    'create late bound dictionary object
    Set oDict = CreateObject("Scripting.Dictionary")
    oDict.comparemode = vbTextCompare
   
    'the data to add
    Set srcRng = srcSht.Cells(1, 1).CurrentRegion
    'skip header row
    For iRow = 2 To srcRng.Rows.Count
       
        'save the key value - which is the file name to open
        sKey = CStr(srcSht.Cells(iRow, 2).Value)
       
        'if the key is already in the dictionary, then Union the other row with the same key
        With srcRng
            If oDict.exists(sKey) Then
                Set rngTemp = oDict(sKey)
                Set rngTemp = Union(rngTemp, .Range(.Cells(iRow, 3), .Cells(iRow, 7)))
                Set oDict(sKey) = rngTemp
            'otherwise just add the key and row
            Else
                Set rngTemp = .Range(.Cells(iRow, 3), .Cells(iRow, 7))
                Set oDict(sKey) = rngTemp
            End If
        End With
    Next iRow
   
    'loop through the dictionary keys & paste
    For Each vKey In oDict.keys
        Set destWB = Workbooks.Open(vKey)
        Set destSht = destWB.Worksheets(1)          ' <--- Specify sheet to use in paste
        Set rngTemp = oDict(vKey)
        rngTemp.Copy Destination:=destSht.Range("A" & Rows.Count).End(xlUp).Offset(1)
    Next vKey
    Set oDict = Nothing
End Sub
Thanks. I'm about to give this a shot.
 
Upvote 0
It should be pasting on sheet2 starting in cell J27 but it's not pasting
I probably did not change that correctly. I changed the following:
Set destSht = destWB.Worksheets("Sheet2") ' <--- Specify sheet to use in paste
Set rngTemp = oDict(vKey)
rngTemp.Copy Destination:=destSht.Range("J" & Rows.Count).End(xlUp).Offset(9, 27)
 
Upvote 0
So for every workbook in the list you want to paste it to a sheet called "Sheet2" and you want all the rows to paste to J27 is that correct ?
If so the Destination should be:
destSht.Range("J27")
Rich (BB code):
rngTemp.Copy Destination:=destSht.Range("J27")
 
Upvote 0
Thank you Alex it worked perfectly (y) . I really appreciate it. :)

Consider this one closed.
 
Upvote 0

Forum statistics

Threads
1,215,214
Messages
6,123,665
Members
449,114
Latest member
aides

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