Loop through Excel files and compare files not working

I have a process that exports two sets of identical Excel files – a first set where users can edit data and a second reference set that enables file compare to identify updates made in the first set. Each file has only one sheet and the file name is the sheet name plus extension.

I need a macro to loop through both sets of files and highlight the updates in the first (edited) set, but I cannot get the loop through the set of edited files to work. I have tried both While Wend and For Each Next.

Also, the For Each Next loop is giving an Invalid Next control variable reference error that I cannot fix.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Sub Compare_Spreadsheets()
Dim editPath, refPath, FiletoProcess As String
Dim wbk As Variant
Dim wbks As Collection
Dim editwbk, refwbk As Workbook
Dim editws, refws As Worksheet
Dim Folder, File, FSO As Object
editPath = "C:pathXLSX_1"
refPath = "C:pathXLSX_2"
'Loop through files in reference folder
'FiletoProcess = Dir(refPath & "*.xlsx")
'While FiletoProcess <> ""
For Each refwbk In FSO.GetFolder(refPath).Files
refwbk.Activate
'Check if file with matching name exists in Edit folder - editwbk.Name = refwbk.Name
If FSO.FileExists(editPath & refwbk.Name) Then
Workbooks.Open editwbk
Else
MsgBox "File not found"
For Each refws In refwbk.Worksheets
For Each editws In editwbk.Worksheets
If editws.Name = refws.Name Then
For Each cell In editws.Range("A1").CurrentRegion
If cell.Value <> refws.Range(cell.Address).Value Then
cell.Interior.Color = vbYellow
MsgBox "Changed value in " & cell.Address & " in sheet " & editws.Name
End If
Next cell
Exit For
End If
Next editws
Exit For
Exit For
'End If
Next
End If
Exit For
'Wend
End Sub
</code>
<code>Sub Compare_Spreadsheets() Dim editPath, refPath, FiletoProcess As String Dim wbk As Variant Dim wbks As Collection Dim editwbk, refwbk As Workbook Dim editws, refws As Worksheet Dim Folder, File, FSO As Object editPath = "C:pathXLSX_1" refPath = "C:pathXLSX_2" 'Loop through files in reference folder 'FiletoProcess = Dir(refPath & "*.xlsx") 'While FiletoProcess <> "" For Each refwbk In FSO.GetFolder(refPath).Files refwbk.Activate 'Check if file with matching name exists in Edit folder - editwbk.Name = refwbk.Name If FSO.FileExists(editPath & refwbk.Name) Then Workbooks.Open editwbk Else MsgBox "File not found" For Each refws In refwbk.Worksheets For Each editws In editwbk.Worksheets If editws.Name = refws.Name Then For Each cell In editws.Range("A1").CurrentRegion If cell.Value <> refws.Range(cell.Address).Value Then cell.Interior.Color = vbYellow MsgBox "Changed value in " & cell.Address & " in sheet " & editws.Name End If Next cell Exit For End If Next editws Exit For Exit For 'End If Next End If Exit For 'Wend End Sub </code>
Sub Compare_Spreadsheets()
Dim editPath, refPath, FiletoProcess As String
Dim wbk As Variant
Dim wbks As Collection
Dim editwbk, refwbk As Workbook
Dim editws, refws As Worksheet
Dim Folder, File, FSO As Object

editPath = "C:pathXLSX_1"
refPath = "C:pathXLSX_2"

'Loop through files in reference folder
'FiletoProcess = Dir(refPath & "*.xlsx")
'While FiletoProcess <> ""

For Each refwbk In FSO.GetFolder(refPath).Files
    refwbk.Activate

    'Check if file with matching name exists in Edit folder - editwbk.Name = refwbk.Name
    If FSO.FileExists(editPath & refwbk.Name) Then
        Workbooks.Open editwbk
        Else
        MsgBox "File not found"

        For Each refws In refwbk.Worksheets
            For Each editws In editwbk.Worksheets
                If editws.Name = refws.Name Then
                    For Each cell In editws.Range("A1").CurrentRegion
                        If cell.Value <> refws.Range(cell.Address).Value Then
                        cell.Interior.Color = vbYellow
                        MsgBox "Changed value in " & cell.Address & " in sheet " & editws.Name
                        End If
                        Next cell
                    Exit For
                End If
            Next editws
            Exit For
        Exit For
    'End If
Next
End If
Exit For

'Wend

End Sub

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật