vba 如何编写 Excel 2013 的电子表格比较脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13702663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:41:58  来源:igfitidea点击:

How to script Excel 2013's Spreadsheet Compare?

excelvbaexcel-vbaoffice-2013excel-2013

提问by 410 gone

I'm trying to incorporate the fancy new Spreadsheet Comparefunction from Excel 2013's Inquire Add-in, into a VBA script.

我正在尝试将Excel 2013 的查询加载项中新颖的电子表格比较功能合并到 VBA 脚本中。

The plan is to have a macro to automate comparison between two spreadsheets with predefined names, and to export all the differences as a new spreadsheet.

计划是有一个宏来自动比较具有预定义名称的两个电子表格,并将所有差异导出为新电子表格。

Without success, to date.

迄今为止,没有成功。

Here's what I've tried so far:

这是我迄今为止尝试过的:

  1. Normally, to learn how to automate some Excel functionality, I use Record Macro.
  2. If that fails, I look down the list of addable references, to see if I'm missing something obvious.
  1. 通常,要了解如何自动化某些 Excel 功能,我会使用 Record Macro。
  2. 如果失败,我会查看可添加引用列表,看看我是否遗漏了一些明显的东西。

Both of those have failed in this case. No code appeared relevant to the Spreadsheet Compare, when I recorded a macro (only the peripheral stuff like cell-select appeared). And none of the addable references looked anything like Spreadsheet Compare.

在这种情况下,这两种方法都失败了。当我记录一个宏时,没有出现与电子表格比较相关的代码(只有像单元格选择这样的外围内容出现)。没有一个可添加的引用看起来像电子表格比较。

So how can I script Excel's 2013 Spreadsheet Compare, from VBA?

那么如何从 VBA 编写 Excel 的 2013 电子表格比较脚本?

回答by JulienVan

I opened a similar questionfor automating the Spreadsheet Compare tool from a .NET application, but I haven't found any other way yet than executing it from command-line.

我打开了一个类似的问题,用于从 .NET 应用程序自动化电子表格比较工具,但除了从命令行执行它之外,我还没有找到任何其他方法。

You can do this from your VBA add-in. All you need is to locate the executable file SPREADSHEETCOMPARE.EXE (usually in C:\Program Files (x86)\Microsoft Office\Office15\DCF) and to execute it in command-line with an instruction file as input argument.

您可以从 VBA 加载项执行此操作。您只需要找到可执行文件 SPREADSHEETCOMPARE.EXE(通常位于 C:\Program Files (x86)\Microsoft Office\Office15\DCF)并在命令行中使用指令文件作为输入参数执行它。

This instruction file must be an ASCII file with the two Excel file paths to compare written in separate lines.

这个指令文件必须是一个 ASCII 文件,两个 Excel 文件路径要分开写在单独的行中。

回答by mercedes

  1. Create a runCompare.cmd file:

    REM Execute from command line spreadsheetcompare.exe 
    REM 
    cd C:\Program Files (x86)\Microsoft Office 2013\Office15\DCF
    spreadsheetcompare.exe  C:\reportNames.txt
    
  2. In C:\reportNames.txt, save in the same line the .xlsx files you wish to compare:

    C:\fileA.xlsx C:\fileB.xlsx
    
  3. Execute runCompare.cmd.

  1. 创建一个 runCompare.cmd 文件:

    REM Execute from command line spreadsheetcompare.exe 
    REM 
    cd C:\Program Files (x86)\Microsoft Office 2013\Office15\DCF
    spreadsheetcompare.exe  C:\reportNames.txt
    
  2. 在 C:\reportNames.txt 中,将要比较的 .xlsx 文件保存在同一行中:

    C:\fileA.xlsx C:\fileB.xlsx
    
  3. 执行 runCompare.cmd。

回答by Archlight

You can't.

你不能。

VBA does not cover add'ins as in this case.

在这种情况下,VBA 不包括插件。

Spreadsheet compare is a 3rd party plugin which got swallowed by Microsoft.

电子表格比较是一个被微软吞并的 3rd 方插件。

If you need scriptable compare you can find those which do for each cell, for each row... kind of thing on the net.

如果您需要可编写脚本的比较,您可以在网上找到为每个单元格、每一行...