Excel 如何编写VBA将一张图纸上的每个单元格与另一张图纸上的每个外壳进行比较?
我试图将一张纸上的每个单元格与另一张纸上的每个单元格进行比较,然后突出显示差异。例如,我想查看一张图纸上的单元格a2是否与另一张图纸上的单元格a2相同。有一次,我能够进行比较,但它只是在整个表中寻找匹配项,而不仅仅是特定的细胞,所以我得到了假阳性。很明显,我遗漏了进行细胞间比较的部分。对于VBA来说,这是我到目前为止拼凑的东西:Excel 如何编写VBA将一张图纸上的每个单元格与另一张图纸上的每个外壳进行比较?,excel,vba,Excel,Vba,我试图将一张纸上的每个单元格与另一张纸上的每个单元格进行比较,然后突出显示差异。例如,我想查看一张图纸上的单元格a2是否与另一张图纸上的单元格a2相同。有一次,我能够进行比较,但它只是在整个表中寻找匹配项,而不仅仅是特定的细胞,所以我得到了假阳性。很明显,我遗漏了进行细胞间比较的部分。对于VBA来说,这是我到目前为止拼凑的东西: Option Explicit Sub test() Dim varSheetA As Variant Dim varSheetB As Vari
Option Explicit
Sub test()
Dim varSheetA As Variant
Dim varSheetB As Variant
Dim strRangeToCheck As String
Dim iRow As Long
Dim iCol As Long
Dim cell As Variant
strRangeToCheck = "A1:Z5000"
Debug.Print Now
varSheetA = Worksheets("RoboReader").Range(strRangeToCheck)
varSheetB = Worksheets("Uploader").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(iRow, iCol) = varSheetB(iRow, iCol) Then
' Cells are identical.
' Do nothing.
Else
Cells.Font.Bold = True
Cells.Font.ColorIndex = 2
Cells.Interior.ColorIndex = 8
Cells.Interior.Pattern = xlSolid
End If
Next iCol
Next iRow
End Sub
逐细胞比较
- 使用不带限定符的单元格时,表示
(1048576*16384)all
Activesheet上的单元格
- 如果在
语句中使用等号(If
),则相反 是(=
)。你可以消除相等的部分,因为没有什么是 发生在那时
Option Explicit
Sub test()
Dim varSheetA As Variant
Dim varSheetB As Variant
Dim strRangeToCheck As String
Dim iRow As Long
Dim iCol As Long
Dim cell As Variant
'Worksheets("Uploader").Cells.ClearFormats
strRangeToCheck = "A1:Z5000"
Debug.Print Now
varSheetA = Worksheets("RoboReader").Range(strRangeToCheck)
varSheetB = Worksheets("Uploader").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(iRow, iCol) <> varSheetB(iRow, iCol) Then
With Worksheets("Uploader").Cells(iRow, iCol)
.Font.Bold = True
.Font.ColorIndex = 2
.Interior.ColorIndex = 8
.Interior.Pattern = xlSolid
End With
End If
Next iCol
Next iRow
End Sub
选项显式
子测试()
Dim varSheetA作为变体
变光板B作为变型
Dim STRANGETOCHECK As字符串
暗淡无光
如长
变暗细胞
'工作表(“上传程序”).Cells.ClearFormats
strRangeToCheck=“A1:Z5000”
调试,现在打印
varSheetA=工作表(“机器人阅读器”)。范围(strRangeToCheck)
varSheetB=工作表(“上传者”)。范围(strRangeToCheck)
调试,现在打印
对于iRow=LBound(varSheetA,1)到UBound(varSheetA,1)
对于iCol=LBound(varSheetA,2)至UBound(varSheetA,2)
如果varSheetA(iRow,iCol)varSheetB(iRow,iCol),则
带有工作表(“上传程序”)。单元格(iRow,iCol)
.Font.Bold=True
.Font.ColorIndex=2
.Interior.ColorIndex=8
.Interior.Pattern=xlSolid
以
如果结束
下一个iCol
下一步
端接头
从我所阅读的内容来看,逻辑看起来不错,你能添加一些示例数据的截图,包括一些误报吗?也可能值得设置一个小范围的示例,并复制一个假阳性,然后逐步通过您的循环,看看为什么它比较不正确。