Excel VBA宏将工作表和输出结果与另一个工作表进行比较

时间:2012-08-10 21:31:06

标签: excel vba excel-vba

我正在使用此处的宏:http://answers.yahoo.com/question/index?qid=20060906110734AA9t2l9来突出显示差异。然而,当有50000行,并且只有2行不同时,它就是大海捞针问题中的一针。

我希望能够做到的是在单独的表格中看到差异,并且前后变化并排。任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

现有代码的少量添加:

Sub Compare_Sheets()

Dim v1,v2
Dim diffRow as long

Set From_WS = Workbooks("Book1").Worksheets("Sheet1")
Set To_WS = Workbooks("Book2").Worksheets("Sheet2")
Set diffWS = Thisworkbook.Sheets("Diff")

diffRow = 1

With From_WS.Cells(1, 1).CurrentRegion
    Total_Rows = .Rows.Count
    Total_Columns = .Columns.Count
End With

For Rows_Counter = 1 To Total_Rows
For Column_Counter = 1 To Total_Columns
v1 = Trim(LCase(From_WS.Cells(Rows_Counter, Column_Counter).Value))
v2 = Trim(LCase(To_WS.Cells(Rows_Counter, Column_Counter).Value)) 

If v1 <> v2 Then
    From_WS.Cells(Rows_Counter, Column_Counter).Interior.ColorIndex = 4
    To_WS.Cells(Rows_Counter, Column_Counter).Interior.ColorIndex = 5

    With diffWS.Rows(diffRow)
        .Cells(1).value=From_WS.Cells(Rows_Counter, Column_Counter).Address()
        .Cells(2).value = v1
        .Cells(3).value= v2
        diffRow = diffRow + 1
    End With

End If

Next Column_Counter
Next Rows_Counter

End Sub