Macro to Extract Duplicate Items on Shet "Summary within sub-folders

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,603
Office Version
  1. 2021
Platform
  1. Windows
I have sheet a sheet called "summary" on workbooks like 'ACCNTS(P).xlsm" within the sub-folders of C:\Pull

I have tried to write code to extract the file Name on Col A where there are duplicate month and years in row1 on sheet "Summary" and extract the duplicate month and years in Col B seperated by a colon, but nothing is being extracted. There is one workbook that has a duplicate month and year

The following workbooks to be excluded when extracting the file name and duplicate month and years


"M_BR1 ACCNTS(P).xlsm" Or Filename Like "M_BR2 ACCNTS(P).xlsm" Or Filename Like "M_NCOR ACCNTS(P).xlsm")


Kinly amend my code accordingly

Code:
 Sub ExtractDuplicatesToNewWorkbook()
    Dim SourceFolderPath As String
    Dim DestWorkbook As Workbook
    Dim DestWorksheet As Worksheet
    Dim SubFolder As Object
    Dim Filename As String
    Dim LastColumn As Long
    Dim LastItem As String
    Dim monthYear As String
    Dim monthYearDict As Object
    
    ' Set the source folder path
    SourceFolderPath = "C:\pull"
    
    ' Create a new workbook for the extracted data
    Set DestWorkbook = Workbooks.Add
    Set DestWorksheet = DestWorkbook.Sheets(1)
    DestWorksheet.Name = "Duplicate Months"
    
    ' Initialize the row index for writing data in the destination sheet
    Dim DestRowIndex As Long
    DestRowIndex = 1
    
    ' Create a dictionary to store month and year combinations
    Set monthYearDict = CreateObject("Scripting.Dictionary")
    
    ' Loop through each direct subfolder in the source folder
    For Each SubFolder In CreateObject("Scripting.FileSystemObject").GetFolder(SourceFolderPath).SubFolders
        ' Reset the dictionary for each subfolder
        monthYearDict.RemoveAll
        
        ' Loop through each file in the subfolder
        Filename = Dir(SubFolder.Path & "\*ACCNTS(P).xlsm")
        Do While Filename <> ""
            ' Check if the workbook should be excluded
            If Not (Filename Like "M_BR1 ACCNTS(P).xlsm" Or Filename Like "M_BR2 ACCNTS(P).xlsm" Or Filename Like "M_NCOR ACCNTS(P).xlsm") Then
                ' Open the workbook without displaying alerts
                Dim SourceWorkbook As Workbook
                Set SourceWorkbook = Workbooks.Open(SubFolder.Path & "\" & Filename, False)
                
                ' Suppress messages and alerts from the workbook
                Application.DisplayAlerts = False
                Application.EnableEvents = False
                
                ' Get the last item in row 1 on the source sheet
                LastColumn = SourceWorkbook.Sheets("Summary").Cells(1, SourceWorkbook.Sheets("Summary").Columns.Count).End(xlToLeft).Column
                LastItem = SourceWorkbook.Sheets("Summary").Cells(1, LastColumn).Value
                
                ' Extract and check month and year
                monthYear = Format(SourceWorkbook.Sheets("Summary").Cells(2, LastColumn).Value, "mm-yyyy")
                If monthYearDict.Exists(monthYear) Then
                    ' Duplicate found, write file name and month-year to destination sheet
                    DestWorksheet.Cells(DestRowIndex, 1).Value = Filename
                    DestWorksheet.Cells(DestRowIndex, 2).Value = monthYear
                    DestRowIndex = DestRowIndex + 1
                Else
                    ' New month-year, add to dictionary
                    monthYearDict.Add monthYear, True
                End If
                
                ' Close the source workbook without saving changes and re-enable alerts
                SourceWorkbook.Close False
                Application.DisplayAlerts = True
                Application.EnableEvents = True
            End If
            
            ' Move to the next file
            Filename = Dir
        Loop
    Next SubFolder
    
    ' Auto-fit the column widths in the destination sheet
    DestWorksheet.Columns("A:B").AutoFit
    
    MsgBox "Duplicate Extraction completed!", vbInformation 
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I have amended my code and it works perfectly

Code:
Sub ExtractDuplicatesToExistingWorkbook()
    Dim SourceFolderPath As String
    Dim DestWorksheet As Worksheet
    Dim SubFolder As Object
    Dim Filename As String
    Dim LastColumn As Long
    Dim LastItem As String
    Dim monthYear As String
    Dim monthYearDict As Object
    
    ' Set the source folder path
    SourceFolderPath = "C:\pull"
    
    ' Set the destination worksheet within the active workbook
    Set DestWorksheet = ThisWorkbook.Sheets("Duplicate Months")
    
    ' Clear existing data in columns A and B
    DestWorksheet.Range("A2:B" & DestWorksheet.Cells(DestWorksheet.Rows.Count, 1).End(xlUp).Row).ClearContents
    
    ' Initialize the row index for writing data in the destination sheet
    Dim DestRowIndex As Long
    DestRowIndex = 2
    
    ' Create a dictionary to store month and year combinations
    Set monthYearDict = CreateObject("Scripting.Dictionary")
    
    ' Loop through each direct subfolder in the source folder
    For Each SubFolder In CreateObject("Scripting.FileSystemObject").GetFolder(SourceFolderPath).SubFolders
        ' Reset the dictionary for each subfolder
        monthYearDict.RemoveAll
        
        ' Loop through each file in the subfolder
        Filename = Dir(SubFolder.Path & "\*ACCNTS(P).xlsm")
        Do While Filename <> ""
            ' Check if the workbook should be excluded
         If Not (Filename Like "M_BR1 ACCNTS(P).xlsm" Or Filename Like "M_BR2 ACCNTS(P).xlsm" Or Filename Like "M_NCOR ACCNTS(P).xlsm") Then
                ' Open the workbook without displaying alerts
                Dim SourceWorkbook As Workbook
                Set SourceWorkbook = Workbooks.Open(SubFolder.Path & "\" & Filename, False)
                
                ' Suppress messages and alerts from the workbook
                Application.DisplayAlerts = False
                Application.EnableEvents = False
                
                ' Get the last column number
                LastColumn = SourceWorkbook.Sheets("Summary").Cells(1, SourceWorkbook.Sheets("Summary").Columns.Count).End(xlToLeft).Column
                
                ' Loop through each month-year combination
                For col = 2 To LastColumn
                    ' Extract and check month and year
                    monthYear = SourceWorkbook.Sheets("Summary").Cells(1, col).Value
                    If monthYearDict.Exists(monthYear) Then
                        ' Duplicate found, write file name and month-year to destination sheet
                        DestWorksheet.Cells(DestRowIndex, 1).Value = Filename
                        DestWorksheet.Cells(DestRowIndex, 2).Value = monthYear
                        DestRowIndex = DestRowIndex + 1
                    Else
                        ' New month-year, add to dictionary
                        monthYearDict.Add monthYear, True
                    End If
                Next col
                
                ' Close the source workbook without saving changes and re-enable alerts
                SourceWorkbook.Close False
                Application.DisplayAlerts = True
                Application.EnableEvents = True
            End If
            
            ' Move to the next file
            Filename = Dir
        Loop
    Next SubFolder
    
    ' Auto-fit the column widths in the destination sheet
    DestWorksheet.Columns("A:B").AutoFit
    
    MsgBox "Duplicate Extraction completed!", vbInformation
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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