Try this.
You will need a sheet named "Compare" in the workbook where the code is running.
Sub Compare()
Dim Rng1 As Range, Rng2 As Range, arr1, arr2, arrOut
Dim rw As Long, col As Long, c As Long, v1, v2
'open workbooks and assign ranges
Set Rng1 = Workbooks.Open("F:LearningBook1.xlsx").Worksheets(1).UsedRange
Set Rng2 = Workbooks.Open("F:LearningBook2.xlsx").Worksheets(1).UsedRange
'check ranges are comparable
If Rng1.Rows.Count <> Rng2.Rows.Count Or _
Rng1.Columns.Count <> Rng2.Columns.Count Then
MsgBox "Ranges are different sizes!"
Exit Sub
End If
'faster to read from arrays...
arr1 = Rng1.Value
arr2 = Rng2.Value
'size array for output (need 3 output columns per input column)
ReDim arrOut(1 To UBound(arr1, 1), 1 To 3 * UBound(arr1, 2))
For rw = 1 To UBound(arr1, 1)
c = 1 'start column position in output array
For col = 1 To UBound(arr1, 2)
v1 = arr1(rw, col)
v2 = arr2(rw, col)
If rw = 1 Then
'column headers here...
arrOut(rw, c) = v1 & "_book1"
arrOut(rw, c + 1) = v2 & "_book2"
arrOut(rw, c + 2) = "Compare"
Else
'column values comparison
arrOut(rw, c) = v1
arrOut(rw, c + 1) = v2
arrOut(rw, c + 2) = IIf(v1 = v2, "Pass", "Fail")
End If
c = c + 3
Next col
Next rw
'put result array on worksheet
With ThisWorkbook.Sheets("Compare")
.UsedRange.ClearContents
.Range("A1").Resize(UBound(arrOut, 1), UBound(arrOut, 2)).Value = arrOut
End With
End Sub
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…