簡體   English   中英

使用VBA比較兩個Excel工作簿

[英]Using VBA to compare two excel workbooks

是的,我知道還有其他一些與此相關的主題,但是我認為創建另一個主題會更好,因為這有所不同。 無論如何,如果有人認為我沒有遵守論壇規則,請執行您必須做的事情。

我下面這個帖子被談論比較兩個工作簿。 因為我想比較兩個具有相同內容的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耗盡內存或變得太慢?

您正在使用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)

直接調用工作表功能將始終引用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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM