[英]Compare two excel workbooks: VBA arrays not equal
我正在嘗試比較兩個excelworkbook,並列出此工作簿中的差異。
我在下面的行中收到錯誤“類型不匹配”:
If varSheetA <> varSheetB Then
碼:
Sub compareworkb()
Dim varSheetA As Variant
Dim varSheetB As Variant
Dim strRangeToCheck As String
Dim iRow As Long
Dim iCol As Long
Set wbkA = Workbooks.Open(Filename:="C:\Solution - Beginners template .xlsx")
Set wbkB = Workbooks.Open(Filename:="C:\Template_Project Lead - Beginners.xlsx")
For i = 1 To wbkA.Sheets.Count
Set varSheetA = wbkA.Worksheets(wbkA.Sheets(i).Name)
Set varSheetB = wbkB.Worksheets(wbkB.Sheets(i).Name)
strRangeToCheck = "A1:N100"
Debug.Print Now
varSheetA = varSheetA.Range(strRangeToCheck)
varSheetB = varSheetB.Range(strRangeToCheck)
Debug.Print Now
For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1)
For iCol = LBound(varSheetA, 2) To UBound(varSheetA, 2)
If varSheetA <> varSheetB Then
wbkB.Sheets(ShName).Cells(iRow, iCol).Interior.Color = vbYellow
ThisWorkbook.Sheets(1).Cells(7 + sh, 2) = "Mismatch Found"
ThisWorkbook.Sheets(1).Cells(7 + sh, 2).Interior.Color = vbYellow
End If
Next
Next
Next i
End Sub
您正在遍歷數組的每個元素,但是每次都嘗試比較整個數組 ,這不能通過<>
來完成。 相反,只需添加您要比較的項目的索引
For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1)
For iCol = LBound(varSheetA, 2) To UBound(varSheetA, 2)
' Note this change vvvvv vvvvvv
If varSheetA(iRow,iCol) <> varSheetB(iRow,iCol) Then
wbkB.Sheets(ShName).Cells(iRow, iCol).Interior.Color = vbYellow
ThisWorkbook.Sheets(1).Cells(7 + sh, 2) = "Mismatch Found"
ThisWorkbook.Sheets(1).Cells(7 + sh, 2).Interior.Color = vbYellow
End If
Next iCol
Next iRow
編輯:
您似乎已經改變了主意,如何循環瀏覽工作表,卻忘記了更新變量名。 比如你正在試圖引用表ShName
,當你還沒有定義ShName
,而你試圖寫行7+sh
當你還沒有定義sh
! 在使用i
進行循環之前,您還沒有將i
聲明為變量。
另外, 當工作表中存在變量類型不匹配時 ,還會收到類型不匹配錯誤! 例如,如果您在一個單元格中有一個錯誤,而在另一張紙上的相應單元格中有一個整數,則無法使用<>
比較這些東西。 在下面的代碼中,我包括了一個變量類型檢查,有關詳細信息,請參見注釋。 該代碼還包含前面提到的更正:
Sub compareworkb()
Dim varSheetA As Variant
Dim varSheetB As Variant
Dim strRangeToCheck As String
Dim iRow As Long
Dim iCol As Long
Dim i As Long
Dim mismatch As Boolean
Set wbkA = ThisWorkbook
Set wbkB = Workbooks("test2.xlsx")
strRangeToCheck = "A1:N100" ' Define this once outside the loop, as it doesn't change
For i = 1 To wbkA.Sheets.Count
varSheetA = wbkA.Worksheets(i).Range(strRangeToCheck)
varSheetB = wbkB.Worksheets(i).Range(strRangeToCheck)
For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1)
For iCol = LBound(varSheetA, 2) To UBound(varSheetA, 2)
mismatch = False
' Check that cell values are the same variable type
If VarType(varSheetA(iRow, iCol)) = VarType(varSheetB(iRow, iCol)) Then
' If they are the same variable type, we can compare them!
If varSheetA(iRow, iCol) <> varSheetB(iRow, iCol) Then
mismatch = True
End If
Else ' If they are not the same variable type, then it must be a mismatch
mismatch = True
End If
' If mismatch found then note it / colour corresponding cell
If mismatch Then
wbkB.Sheets(i).Cells(iRow, iCol).Interior.Color = vbYellow
ThisWorkbook.Sheets(1).Cells(7 + i, 2) = "Mismatch Found"
ThisWorkbook.Sheets(1).Cells(7 + i, 2).Interior.Color = vbYellow
End If
Next iCol
Next iRow
Next i
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.