

Private Function BuildDataDictionary(ByRef data As Variant, _ The BuildDataDictionary function will accept your memory-based array and use the selected column of data as a unique key (currently defaulted to column "I"). As with the previous point, this is a perfect opportunity to isolate the logic in a separate function.
#Compare two columns in excel in different sheets code
Breaking out code into separate routines is very helpful because it makes your main logic easier to follow.

Give this function a worksheet and optionally the starting row or column and it determines the range of available data, returning a memory-based array. This is especially useful if you have repetitive logic where only the variable is different. When your routine starts getting very long, that is the perfect time to begin breaking parts of the logic out into separate functions/subs. You'll see that there is a call to a BuildDataArrays function.

The first picture shows how I call the sub to compare the two sheets I add the code into a module and then create a button and do the following. MsgBox difference & " cells contain different data! ", vbInformation, "Comparing Two Worksheets"

'Application.Calculation = xlCalculationAutomatic FormatConditions().SetFirstPriorityĭebug.Print "Report Generated secs " & Timer - tm FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="=""""" '"""""""" 'Application.Calculation = xlCalculationManualĪrr3(row, col) = Arr1(row, col) & " " & Arr2(row, col) Sub Compare2WorkSheets(ws1 As Worksheet, ws2 As Worksheet)ĭim ws1row As Long, ws2row As Long, ws1col As Integer, ws2col As Integerĭim maxrow As Long, maxcol As Integer, colval1 As String, colval2 As Stringĭim Report As Workbook, difference As Longĭim Arr1 As Variant, Arr2 As Variant, Arr3 As Variant, Rng As Range The pictures below show what I'm trying to achieve with this code In case it does find the name, then it compares all the columns of that row and if something different it shows the difference in report with color red. If it doesn’t find the file it just copies that entire row including all the columns to the report file (file that is created) It goes to “Name” and then searches for that name is the other file. I’m trying to modify the Code so it does the following: It compares two excel sheets cell by cell and copies the differences into another excel file. This code is a result of a lot of help from this community.
