Macro to open files in a folder, copy data if there is a match, close them

sgeng4

New Member
Joined
Dec 9, 2017
Messages
28
Hi all,

I'm looking for some help making a macro.

I have a bunch of files in a folder. I want a macro that opens up each file one by one, and if the names match, then copy the value, date and certificate associated with that name onto the master spreadsheet.


So in the folder, I have excel files that have the following information in each sheet in the second tab:


CertificateA8
dateDec 9
name21.3
name31.5
name63.0

In the master sheet, I have the following columns:
UNIQUENAMEVALUEDATECERTIFICATE
name1
name2
name3

I am looking for a way for excel to automatically fill in the value, date, and certificate numbers by looking them up in the sheets and pasting them into the correct row. For example, like so:




UNIQUENAMEVALUEDATECERTIFICATE
name1
name21.3Dec 9A8
name31.5Dec 9A8



So far, I got the macro to open the file folder, and open the files one by one, but I don't know how to code it so it searches for matches, and copies the correct cells over.

Code:
Sub CompleteMasterSpreadsheet()
'DECLARE AND SET VARIABLES
Dim wbk As Workbook
Dim FileName As String
Dim Path As String


With Application.FileDialog(msoFileDialogFolderPicker)
    .Show
    Path = .SelectedItems(1) & "\"
End With

FileName = Dir(Path & "*.xls*")

'Loop to open excel files

 Do While Len(FileName) > 0  'IF NEXT FILE EXISTS THEN
    Set wbk = Workbooks.Open(Path & FileName)
    '
    ' CODE to search for matches, copy them if there is a match
    '

    
    ' end of code

     
    wbk.Close True
    FileName = Dir
Loop
End Sub

Help is really appreciated!

Thank you. =)
 
It seems to only work if I copy and paste the information from the original files into a new sheet. The names in the master sheet are EXACTLY the same. I've checked it a few times with protection and locking the cells, and also came to the same conclusion. I can't seem to figure out why it's not copying the information from the original files.
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Without being able to see your files it's very difficult to help.
Would you be willing to upload your master file & one of the other files to either dropbox, or OneDrive, mark for sharing & then paste a link to this thread?
 
Upvote 0
Thanks for the files. The problem is that sheets(1) is hidden, so the macro was looking at the wrong sheets.
Try this
Code:
Sub CompleteMasterSpreadsheet()
   'DECLARE AND SET VARIABLES
   Dim wbk As Workbook
   Dim Sht As Worksheet
   Dim FileName As String
   Dim Path As String
   Dim Cl As Range
   Dim Rng As Range
   Dim Cnt As Long
   
Application.ScreenUpdating = False

   With Application.FileDialog(4)
       .Show
       Path = .SelectedItems(1) & "\"
   End With
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp)) '<< Looks in col A of the master sheet for the names
         If Not .exists(Cl.Value) Then .Add Cl.Value, Cl '<< Adds the names to a dictionary
      Next Cl
      
      FileName = Dir(Path & "*.xls*")
      
       Do While Len(FileName) > 0
         Set wbk = Workbooks.Open(Path & FileName)
         Cnt = Cnt + 1
         Set Sht = wbk.Sheets("[COLOR=#0000ff]Values[/COLOR]")
         For Each Rng In Sht.Range("B7", Sht.Range("B" & Rows.Count).End(xlUp)) '<< Looks in col B of the new wbk for the names
            If .exists(Rng.Value) Then '<< checks if the name is in the dictionary (ie in the Master sheet)
               .Item(Rng.Value).Offset(, 5).Value = Rng.Offset(, 5).Value              '<< adds the value to the master
               .Item(Rng.Value).Offset(, 10).Value = wbk.Sheets("[COLOR=#0000ff]Shift[/COLOR]").Range("C7").Value   '<< adds the date to the master
               .Item(Rng.Value).Offset(, 9).Value = wbk.Sheets("[COLOR=#0000ff]Shift[/COLOR]").Range("F7").Value    '<< add the certificate to the master
            End If
         Next Rng
         wbk.Close False
         FileName = Dir
      Loop
   End With
   MsgBox Cnt
End Sub
This assumes that each file you're opening has a sheet called Values & a sheet called Shift
 
Upvote 0
Thank you so so so much! I would have never realized that the sheet was hidden. You're amazing!! I added a few lines so it unhides the worksheets instead of using the name Values and Shift, just in case some of the files don't have the same names or is missing a capital, etc.

Thank you so much for sticking with me too. =)

Code:
Sub CompleteMasterSpreadsheet()
   'DECLARE AND SET VARIABLES
   Dim wbk As Workbook
   Dim Sht As Worksheet
   Dim FileName As String
   Dim Path As String
   Dim Cl As Range
   Dim Rng As Range
   Dim Cnt As Long
   
Application.ScreenUpdating = False


   With Application.FileDialog(4)
       .Show
       Path = .SelectedItems(1) & "\"
   End With
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp)) '<< Looks in col A of the master sheet for the names
         If Not .exists(Cl.Value) Then .Add Cl.Value, Cl '<< Adds the names to a dictionary
      Next Cl
      
      FileName = Dir(Path & "*.xls*")
      
       Do While Len(FileName) > 0
         Set wbk = Workbooks.Open(Path & FileName)


'unhide all worksheets
             For Each WS In ActiveWorkbook.Worksheets
             WS.Visible = xlSheetVisible
             Next WS


         Cnt = Cnt + 1
         Set Sht = wbk.Sheets("Values")
         For Each Rng In Sht.Range("B7", Sht.Range("B" & Rows.Count).End(xlUp)) '<< Looks in col B of the new wbk for the names
            If .exists(Rng.Value) Then '<< checks if the name is in the dictionary (ie in the Master sheet)
               .Item(Rng.Value).Offset(, 5).Value = Rng.Offset(, 5).Value              '<< adds the value to the master
               .Item(Rng.Value).Offset(, 10).Value = wbk.Sheets("Shift").Range("C7").Value   '<< adds the date to the master
               .Item(Rng.Value).Offset(, 9).Value = wbk.Sheets("Shift").Range("F7").Value    '<< add the certificate to the master
            End If
         Next Rng
         wbk.Close False
         FileName = Dir
      Loop
   End With
   MsgBox Cnt
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,201
Messages
6,123,617
Members
449,109
Latest member
Sebas8956

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