比较VBA中2个文件的颜色和格式

时间:2018-08-06 12:32:39

标签: excel vba compare

我目前正在处理一个宏,以比较两个应该相同的大文件。 宏的要点是查找它们是否是使文件之间的颜色,公式或格式不一致的差异。我制作了一个可以逐个单元比较的程序,但是运行需要5个小时...

我尝试使用variant,但是我只能比较值。

这是代码的一部分,我想添加Interior.ColorIndex和.Format:

For colonne = 1 To dercolonne
For ligne = 1 To derligne
Windows(WB_1).Activate
Sheets(wsname).Activate
MontabA = Range(Cells(1, 1), Cells(derligne, dercolonne)).FormulaR1C1Local
contenue1 = MontabA(ligne, colonne)
If Err > 0 Then
erreur = erreur & sh.Name & " " & Cells(ligne, colonne).Address & ": erreur cellule" & vbCrLf
End If
Windows(WB_2).Activate
Sheets(wsname).Activate
MontabB = Range(Cells(1, 1), Cells(derligne, dercolonne)).FormulaR1C1Local
contenue2 = MontabB(ligne, colonne)
If contenue1 <> contenue2 Then
erreur = erreur & sh.Name & ": " & Cells(derligne, dercolonne).Address & vbCrLf
Else
End If
Next ligne
Next colonne

非常感谢,

昆汀

1 个答案:

答案 0 :(得分:0)

这样的事情会更快,但这实际上取决于您需要进行多少次比较以及达到何种详细程度。

Dim ws1, ws2, c1, c2

Set ws1 = Workbooks(WB_1).Sheets(wsname)
Set ws2 = Workbooks(WB_2).Sheets(wsname)

MontabA = ws1.Range(ws1.Cells(1, 1), ws1.Cells(derligne, dercolonne)).FormulaR1C1Local
MontabB = ws2.Range(ws2.Cells(1, 1), ws2.Cells(derligne, dercolonne)).FormulaR1C1Local

For colonne = 1 To dercolonne
    For ligne = 1 To derligne

        Set c1 = ws1.Cells(ligne, colonne)
        Set c2 = ws2.Cells(ligne, colonne)

        If MontabA(ligne, colonne) <> MontabA(ligne, colonne) Then
            'add content error
        End If

        If c1.Interior.Color <> c2.Interior.Color Then
            'add color error
        End If

        'etc other tests here

    Next ligne
Next colonne