简体   繁体   English

用于比较VBA-Excel工作簿的宏

[英]Macro to compare workbooks for VBA- Excel

I am fairly new to VBA for excel. 我对VBA来说还很陌生。 I would like to create a macro to compare two separate Workbooks, then show the differences between them. 我想创建一个宏来比较两个单独的工作簿,然后显示它们之间的差异。 For more detail i will say that the only thing that needs to be compared are Column A in both of the workbooks. 有关更多详细信息,我将说,唯一需要比较的是两个工作簿中的A列。 Any advice will be greatly appreciated. 任何建议将不胜感激。

This will do what you want!! 这会做你想要的!

Public Sub CompareSheets()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim cell As Range, rng As Range
Set ws1 = Workbooks.Open("C:\your_path_here\Book1.xlsx").Sheets(1)
Set ws2 = Workbooks.Open("C:\your_path_here\Book2.xlsx").Sheets(1)
Set rng = ws1.Range("A1:A20")
For Each cell In rng
    Celladdress = cell.Address
    If cell <> ws2.Range(Celladdress) Then
        cell.Interior.Color = vbYellow
        ws2.Range(Celladdress).Interior.Color = vbYellow
    End If
Next cell
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM