所以我想比较两个不同的工作簿在Excel中与VBA。我知道应用程序“比较电子表格”确实存在,但我需要一个通用代码。我在论坛上寻找了很多,试图找到一个解决方案,但没有人符合我的需求。比较两个不同的工作簿和突出差异excel vba
我想要的是:基本上是一个脚本:要求用户另一个excelfile(file2)与当前文件(file1)进行比较。然后,差异需要在当前文件中突出显示。
我是一种初学者,所以请温和:)我有一些代码,我正在尝试使用,但它只适用于同一工作簿中两张纸的比较。
如果有人得到了一些能够完成这项工作的代码,或者让我更进一步,那么分享代码真的很友善。或者如果你有一些关于如何使代码低于我的需求的提示。非常感谢!!
`Sub test()
Dim varSheetA As Variant
Dim varSheetB As Variant
Dim strRangeToCheck As String
Dim iRow As Long
Dim iCol As Long
strRangeToCheck = "A1:IV65536"
' If you know the data will only be in a smaller range, reduce the size of the ranges above.
Debug.Print Now
varSheetA = Worksheets("Sheet1").Range(strRangeToCheck)
varSheetB = Worksheets("Sheet 1").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
'MsgBox ("No difference")
' Cells are identical.
' Do nothing.
Else
HighlightCells
' Cells are different.
' Code goes here for whatever it is you want to do.
End If
Next iCol
Next iRow
结束子 ' 这是我试图做的,以使作为不同粗体或只是提示用户的部分(一个或多个)所述的修改:
子试验()
Dim varSheetA As Variant
Dim varSheetB As Variant
Dim strRangeToCheck As String
Dim iRow As Long
Dim iCol As Long
strRangeToCheck = "A1:N70"
' If you know the data will only be in a smaller range, reduce the size of the ranges above.
Debug.Print Now
varSheetA = Worksheets("229019_PSS360_17w15").Range(strRangeToCheck)
varSheetB = Worksheets("229019_PSS360_Ny").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
Selection.Font.BOLD = True
'MsgBox ("diff")
' Cells are identical.
' Do nothing.
Else
'MsgBox ("same")
' Cells are different.
' Code goes here for whatever it is you want to do.
End If
Next iCol
Next iRow
结束子
我看到你几乎从我的[早期回答](https://stackoverflow.com/a/5388488/119775)复制粘贴代码。你是否尝试修改它以满足你的需求?你这样做时遇到的具体问题是什么?让我们知道,然后我们可以提供帮助。 –
嘿,谢谢你的回答! 我不是故意复制和粘贴你的代码就是这样,我的意思是让人们知道我在做什么,我发现你的代码真的很好。 您的代码非常好,但我试图将两个工作簿相互比较,代码仅适用于两个工作表。 在你的答案你没有提供一种方式,其中你可以比较不同的工作簿,但我并没有得到它的工作,使其再次 感谢askes另一个文件中的用户对你的答案 – zejton
“并没有得到它的工作“ - >告诉我们你试过什么,什么不行,然后我们可以提供帮助。 –