简体   繁体   English

比较两个Excel工作表的内容

[英]compare contents of two excel worksheets

I have two worksheets in the same workbook. 我在同一工作簿中有两个工作表。 The first worksheet contains last periods score matrix, the second worksheet contains this periods score matrix. 第一个工作表包含最后一个期间分数矩阵,第二个工作表包含这个期间分数矩阵。

I am struggling to find a away to highlight those cells in this periods worksheet that are different from last periods worksheet. 我正在努力寻找一个可以突出显示此期间工作表中与上一个期间工作表不同的单元格的地方。

I have been able to go as far as identifying the changed cells. 我已经能够找出已更改的单元格。 I know that works from use of 'MsgBoxes', however I cannot find a way to highlight the identified cells. 我知道这可以通过使用“ MsgBoxes”来实现,但是我找不到突出显示已识别单元格的方法。 Its probably because I have chosen to go about this the wrong way entirely. 这可能是因为我完全选择了错误的方法。 Can someone please give me a guide as to how I should go about this? 有人可以给我指导我应该怎么做吗?

The code I have that works (according to MsgBox's anyway) is below. 下面是我拥有的代码(无论如何,根据MsgBox的代码)。 I would very much appreciate any guidance whatsoever. 我将非常感谢任何指导。 Thanks, 谢谢,

    Option Explicit
Sub B_HighlightDifferences()
'Workbooks("Scoring Matrix NEW").Activate
    Dim varScoring As Variant
    Dim varScoring_OLD As Variant
    Dim strRangeToCheck As String
    Dim irow As Long
    Dim icol As Long
    Dim color As CellFormat
    strRangeToCheck = "bl9:bo15"  'smallrange for testing purposes only
    varScoring = Worksheets("Scoring").Range(strRangeToCheck)
    varScoring_OLD = Worksheets("Scoring_OLD").Range(strRangeToCheck)
    For irow = LBound(varScoring, 1) To UBound(varScoring, 1)
        For icol = LBound(varScoring, 2) To UBound(varScoring, 2)
            If varScoring(irow, icol) = varScoring_OLD(irow, icol) Then
                ' Cells are identical. ' Do nothing.
                MsgBox "This has not changed"
            Else
                ' Cells are different. 
        ' Need code here to highlight each cell that is different
                 MsgBox "This has changed"
End If
            End If
        Next icol
    Next irow
End Sub

You did most of the hard work. 您完成了大部分艰苦的工作。 I would change the following. 我将更改以下内容。 Add: 加:

dim newCell as Range
Application.ScreenUpdating = False

... then inside your for loop: ...然后在您的for循环中:

Set newCell = varScoring.Cells(irow, icol)

Then you should be able to apply any formatting you want to newCell (which is a Range object) when you find it's different. 然后,当发现不同的格式时,应该可以将所需的任何格式应用于newCell (这是一个Range对象)。

newCell.Select
With Selection.Interior
    .Color = 49407
    ' any formatting you want.
End With

At the end of your routine, turn screen updating on again: 在例程结束时,再次打开屏幕更新:

Application.ScreenUpdating = True

Let me know if this makes sense. 让我知道这是否有意义。

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

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