Differences Tool

S

Scott Weiss

Does anyone know of a differences tool for comparing
embedded VBA code between two .xls files for Excel 2002?

Thanks,
Scott
 
B

Bernie Deitrick

Scott,

The code below will compare the code in two currently opened workbooks. You
can modify this to do a line by line comparison if you like, but even a
single inserted line will break that kind of comparison. There are code
comparison tools available on the web that are smarter, but this will at
least highlight changes. I also haven't really checked to see what
limitations there are on module size - probably fairly small, since the code
is read into a single string variable. But, all those limitations can be
fixed - it jsut depends on your interest in doing it.

The code requires a reference to MS VBA extensibility, and assumes that the
two workbooks are copies of the same template, and have identical codemodule
names.

HTH,
Bernie
MS Excel MVP

Sub CompareCodeInModule()
Dim myCode1 As String
Dim myCode2 As String
Dim WB1 As Workbook
Dim WB2 As Workbook
Dim myMod As VBComponent

Set WB1 = Application.Windows(1).ActiveSheet.Parent
Set WB2 = Application.Windows(2).ActiveSheet.Parent
MsgBox WB1.Name
MsgBox WB2.Name

For Each myMod In WB1.VBProject.VBComponents
With myMod.CodeModule
myCode1 = .Lines(1, .CountOfLines)
' Uncomment the next line to see the code in a msgbox
' MsgBox myCode1
With WB2.VBProject.VBComponents(myMod.Name).CodeModule
myCode2 = .Lines(1, .CountOfLines)
' Uncomment the next line to see the code in a msgbox
' MsgBox myCode2
End With
If myCode1 = myCode2 Then
MsgBox "Code's the same in " & myMod.Name
Else
MsgBox "There's a difference in " & myMod.Name
End If
End With
Next myMod

End Sub
 
Top