比较行和报告差异Excel VBA
我一直在尝试使用下面的代码来比较两个Excel工作表,但是我无法让它完全按照我的需要运行。我需要逐行比较,并报告特定的差异,即使每张工作表上的数据行顺序不一定相同。代码根据数据在表中的物理存在方式报告所有差异。所以它显示了差异,但如果数据在每个表中按相同的顺序排列,它们实际上不会有差异。由于数据的性质,我不能先排序。希望这是有意义的。有人能告诉我需要什么样的改变才能得到我所需要的吗比较行和报告差异Excel VBA,excel,vba,Excel,Vba,我一直在尝试使用下面的代码来比较两个Excel工作表,但是我无法让它完全按照我的需要运行。我需要逐行比较,并报告特定的差异,即使每张工作表上的数据行顺序不一定相同。代码根据数据在表中的物理存在方式报告所有差异。所以它显示了差异,但如果数据在每个表中按相同的顺序排列,它们实际上不会有差异。由于数据的性质,我不能先排序。希望这是有意义的。有人能告诉我需要什么样的改变才能得到我所需要的吗 Sub Compare() ' compare two different worksheets in t
Sub Compare()
' compare two different worksheets in the active workbook
CompareWorksheets Worksheets("Sheet1"), Worksheets("Sheet2")
' compare two different worksheets in two different workbooks
'CompareWorksheets ActiveWorkbook.Worksheets("Sheet1"), _
' Workbooks("impchk1.xls").Worksheets("Sheet2")
End Sub
Sub CompareWorksheets(ws1 As Worksheet, ws2 As Worksheet)
Dim r As Long, c As Integer
Dim lr1 As Long, lr2 As Long, lc1 As Integer, lc2 As Integer
Dim maxR As Long, maxC As Integer, cf1 As String, cf2 As String
Dim rptWB As Workbook, DiffCount As Long
Application.ScreenUpdating = False
Application.StatusBar = "Creating the report..."
Set rptWB = Workbooks.Add
Application.DisplayAlerts = False
While Worksheets.Count > 1
Worksheets(2).Delete
Wend
Application.DisplayAlerts = True
With ws1.UsedRange
lr1 = .Rows.Count
lc1 = .Columns.Count
End With
With ws2.UsedRange
lr2 = .Rows.Count
lc2 = .Columns.Count
End With
maxR = lr1
maxC = lc1
If maxR < lr2 Then maxR = lr2
If maxC < lc2 Then maxC = lc2
DiffCount = 0
For c = 1 To maxC
Application.StatusBar = "Comparing cells " & Format(c / maxC, "0 %") & "..."
For r = 1 To maxR
cf1 = ""
cf2 = ""
On Error Resume Next
cf1 = ws1.Cells(r, c).FormulaLocal
cf2 = ws2.Cells(r, c).FormulaLocal
On Error GoTo 0
If cf1 <> cf2 Then
DiffCount = DiffCount + 1
Cells(r, c).Formula = "'" & cf1 & " <> " & cf2
End If
Next r
Next c
Application.StatusBar = "Formatting the report..."
With Range(Cells(1, 1), Cells(maxR, maxC))
.Interior.ColorIndex = 19
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
On Error Resume Next
With .Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
On Error GoTo 0
End With
Columns("A:IV").ColumnWidth = 20
rptWB.Saved = True
If DiffCount = 0 Then
rptWB.Close False
End If
Set rptWB = Nothing
Application.StatusBar = False
Application.ScreenUpdating = True
MsgBox DiffCount & " cells contain different formulas!", vbInformation, _
"Compare " & ws1.Name & " with " & ws2.Name
End Sub
子比较()
'比较活动工作簿中的两个不同工作表
比较工作表工作表(“表1”)、工作表(“表2”)
'比较两个不同工作簿中的两个不同工作表
'CompareWorksheets ActiveWorkbook.Worksheets(“Sheet1”)_
“工作手册”(“impchk1.xls”)。工作表(“表2”)
端接头
子比较工作表(ws1作为工作表,ws2作为工作表)
Dim r为长,c为整数
尺寸lr1为长,lr2为长,lc1为整数,lc2为整数
Dim maxR为长,maxC为整数,cf1为字符串,cf2为字符串
将rptWB作为工作簿进行Dim,将DiffCount设置为Long
Application.ScreenUpdating=False
Application.StatusBar=“正在创建报告…”
设置rptWB=工作簿。添加
Application.DisplayAlerts=False
而工作表。计数>1
工作表(2).删除
温德
Application.DisplayAlerts=True
使用ws1.UsedRange
lr1=.Rows.Count
lc1=.Columns.Count
以
使用ws2.UsedRange
lr2=.Rows.Count
lc2=.Columns.Count
以
maxR=lr1
maxC=lc1
如果maxR
在将要更新的工作表中,将每一行转换为字符串并保存到字典中。然后,在要更新的工作表中,遍历所有行,获取它们的字符串代表,并查看字典中是否存在该字符串代表。如果没有,则添加它们
下面是一个示例代码,它从行值中获取字符串
Sub getRowAsString()
Dim cell As Object
Dim sheet As Worksheet
Dim str As String
Dim arr() As Variant
Dim arr2() As Variant
Dim printCol As Integer
Set sheet = ActiveSheet
printCol = sheet.UsedRange.Columns.Count + 1
For Each cell In sheet.UsedRange.Rows
arr = cell.Value2
ReDim arr2(LBound(arr, 2) To UBound(arr, 2))
For i = LBound(arr, 2) To UBound(arr, 2)
arr2(i) = arr(1, i)
Next i
str = Join(arr2, ", ")
ActiveSheet.Cells(cell.Row, printCol).value = str
Next cell
End Sub
下面是一个使用字典的示例:
Sub createDictionary()
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
Key = "hello"
value = "world"
dict.Add Key, value
MsgBox "key exists: " & dict.exists(Key) & vbNewLine & "value: " & dict(Key)
End Sub
如果行的字符串表示太大,可以将其哈希值保存到字典中,以使其更易于管理。是一篇提供VBA代码对字符串进行散列的文章如果没有某种“键”来标识工作表之间的“相同”数据行,则无法实际执行此操作。是否有某些列或列的组合在每条记录中应该是唯一的?^如果要查找行中的差异,如何才能识别该行?如果行不相同,顺序也不相同,那么是什么?您需要某种方式将sheet1中的行映射到Sheet2Tanks中的行,以便发表评论。所有行都是唯一的,所以这就是为什么我认为这是可能的。我需要找出那些不匹配的,并在可能的情况下强调原因。示例数据,A列、B列和E列的组合应该是唯一的。感谢您的回复,我将尝试一下。