使用VBA比较两个excel工作簿

时间:2014-10-08 10:28:58

标签: excel vba excel-vba

是的,我知道还有一些与此相关的主题,但我认为如果我创建另一个主题会更好,因为这有点不同。无论如何,如果有人认为我没有遵守论坛规则,请做你必须做的事。

我正在关注这个正在讨论比较两本工作簿的post。由于我想比较两个具有相同内容的excel文件,我制作了一个非常相似的代码。但是,我的代码似乎没有比较这两个文件,而是将文件A与文件A或文件B与文件B进行比较。

代码的作用是获取两个工作簿,获取名为“资产负债表”的工作表,然后比较两个工作簿中的资产负债表是否具有相同的值。因此,我们不必遍历所有单元格,将工作表加载到Variant数组中。您可以看到资产负债表的图片,只是为了有一个想法: http://i.stack.imgur.com/tc8Nr.png

我的代码就是这个:

Sub CompareWorkbooks()

Dim varSheetA As Variant
Dim varSheetB As Variant
Dim strRangeToCheck As String
Dim iRow As Long
Dim iCol As Long

nlin = 1
ncol = 1

'Get the worksheets from the workbooks
Set wbkA = Workbooks.Open(Filename:="C:\Users\Desktop\BalanceSheet.xls")
Set varSheetA = wbkA.Worksheets("Balance sheet") ' or whatever sheet you need

Set wbkB = Workbooks.Open(Filename:="C:\Users\Desktop\BalanceSheet_old.xls")
Set varSheetB = wbkB.Worksheets("Balance sheet") ' or whatever sheet you need

strRangeToCheck = "B6:D49"
' If you know the data will only be in a smaller range, reduce the size of the ranges above.
Debug.Print Now
varSheetA = Worksheets("Balance Sheet").Range(strRangeToCheck)
varSheetB = Worksheets("Balance Sheet").Range(strRangeToCheck) ' or whatever your other sheet is.
Debug.Print Now

For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1)
    For iCol = LBound(varSheetA, 2) To UBound(varSheetA, 2)
        If varSheetA(iRow, iCol) = varSheetB(iRow, iCol) Then
            ' Cells are identical.
            ' Do nothing.
        Else
           ' Cells are different. Let's fill our main template with the information
            Windows(MainTemplate.xlsm).Activate
            Cells(nlin, ncol) = varSheetA(iRow, 2) 'Gives the name of the changed field
            Cells(nlin, ncol + 1) = varSheetA(iRow, iCol) 'Gives me the value in workbookA
            Cells(nlin, ncol + 2) = varSheetB(iRow, iCol) 'Gives me the value in workbookB
            Cells(nlin, ncol + 3) = nlin 'Gives me the row location
            Cells(nlin, ncol + 4) = ncol 'Gives me the column location
            nlin = nlin + 1
        End If
    Next
Next

End Sub

任何人都可以猜出错误在哪里吗?为什么我没有得到不同的细胞?

另外,我发现了一个新问题,我是否可以在不必提供特定名称的情况下浏览所有工作表?在我的代码中,我必须插入“资产负债表”作为工作表名称,但如果我有几个工作表怎么办?即使通过循环,有没有人对此有好主意,这不会使我的excel耗尽内存或变得太慢?

1 个答案:

答案 0 :(得分:2)

您正在使用workbookA,因此请设置sheetB。

Set varSheetB = wbkA.Worksheets("Balance sheet") ' or whatever sheet you need

应该是:

Set varSheetB = wbkB.Worksheets("Balance sheet") ' or whatever sheet you need

抱歉,我第一次错过了这个:

varSheetA = Worksheets("Balance Sheet").Range(strRangeToCheck)
varSheetB = Worksheets("Balance Sheet").Range(strRangeToCheck)

应该是:

varSheetA = varSheetA.Range(strRangeToCheck)
varSheetB = varSheetB.Range(strRangeToCheck)

直接调用Worksheets函数将始终引用ActiveWorkbook。 而是始终从相关对象(wkbA.Worksheets(“...”)或wkbB.Worksheets(“...”))中调用它。

顺便说一下。您还可以将两个作业合并为一个:

strRangeToCheck = "B6:D49"
Set wbkA = Workbooks.Open(Filename:="C:\Users\Desktop\BalanceSheet.xls")
Set varSheetA = wbkA.Worksheets("Balance sheet").Range(strRangeToCheck)
Set wbkB = Workbooks.Open(Filename:="C:\Users\Desktop\BalanceSheet_old.xls")
Set varSheetB = wbkB.Worksheets("Balance sheet").Range(strRangeToCheck)