【问题标题】:Compare two excel VBA projects比较两个 excel VBA 项目
【发布时间】:2017-08-09 11:27:14
【问题描述】:
我正在 Excel 2010 中编写一些 VBA 宏。
源代码由 TortoiseHG 管理。
但是我找不到比较 2 个不同提交的平滑方法,因为 TortoiseHG 只显示完整的 excel 文件,而不是我正在编写代码的不同 VBA 文件。
如果有人以一种很好而顺利的方式完成了这项工作,我会很高兴得到一些想法。
也可以使用 BeyondCompare 等外部工具。
谢谢!
【问题讨论】:
标签:
excel
compare
tortoisehg
beyondcompare3
vba
【解决方案1】:
对于 VBA 代码:
出于代码比较的目的,我建议导出 VBA 模块并签入.bas(常规模块)和.cls(类模块)文件,然后签入.xlsm 旁边的文件。
那些导出的 VBA 模块实际上为您提供了纯文本版本的代码。工作表/工作簿模块中的代码也可以导出为.cls 文件。这将允许 TortoiseHG 比较实际代码而不是 Excel 文件本身。
对于 Excel 文件本身的比较:
为了比较实际的 XLSM,您可以考虑保存文件,将扩展名更改为 .zip 并提取 zips 内容。
2007 及更高版本的 Excel 文件实际上是包含定义整个工作簿的 XML 的存档和用于 VBA 项目的.bin 文件。
您可能希望通过 XMLLint 或类似工具提取上述 xml,因为默认情况下它们的打印效果并不好。
旁注:
您要求的是一种又好又流畅的方式 - 我想说 Excel 本身没有。您可以使用 VBA 在 Workbook_BeforeClose() 事件上执行这些模块导出,但您会遇到一些安全问题 - 默认情况下,您不允许从内部访问 VBProject。
当然,有一些第三方工具可以比较 VBA 代码而无需提取模块,但是“使用什么”将严重基于意见 - BeyondCompare 确实是一种选择。
希望这会有所帮助。
【解决方案3】:
您可以导出每个模块,并使用 Windows 原生 fc 命令进行比较。
fc /N Module1 Module2 > Result.txt
帮助:
fc /?
Compares two files or sets of files and displays the differences between
them
FC [/A] [/C] [/L] [/LBn] [/N] [/OFF[LINE]] [/T] [/U] [/W] [/nnnn]
[drive1:][path1]filename1 [drive2:][path2]filename2
FC /B [drive1:][path1]filename1 [drive2:][path2]filename2
/A Displays only first and last lines for each set of differences.
/B Performs a binary comparison.
/C Disregards the case of letters.
/L Compares files as ASCII text.
/LBn Sets the maximum consecutive mismatches to the specified
number of lines.
/N Displays the line numbers on an ASCII comparison.
/OFF[LINE] Do not skip files with offline attribute set.
/T Does not expand tabs to spaces.
/U Compare files as UNICODE text files.
/W Compresses white space (tabs and spaces) for comparison.
/nnnn Specifies the number of consecutive lines that must match
after a mismatch.
[drive1:][path1]filename1
Specifies the first file or set of files to compare.
[drive2:][path2]filename2
Specifies the second file or set of files to compare.
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/fc