简体   繁体   English

如何使用VBA导出工作表按钮和详细信息

[英]How to Export Sheet Buttons and Details with VBA

I've been messing around with importing/exporting VBA modules and classes in an Excel app that I work on. 我一直在忙于在我使用的Excel应用程序中导入/导出VBA模块和类。 Seems really useful to use with version control. 似乎与版本控制一起使用非常有用。 I like it. 我喜欢。 I can easily implement code changes in modules, export my changes, commit and push my changes, and "reload" (delete all modules and re-import) my changes in another instance of the Excel app. 我可以轻松地在模块中实现代码更改,导出更改,提交并推送更改,以及在其他Excel应用程序实例中“重新加载”(删除所有模块并重新导入)更改。

However, I can't find a way to use this with sheets. 但是,我找不到在工作表上使用此方法。 If I add a visual change to a sheet (say, I add a button or change the way the sheet looks), is there a way to export the sheet as code, just like modules, so that I can then import the sheet exactly as it looked from my changes? 如果我在工作表上添加了视觉更改(例如,我添加了按钮或更改了工作表的外观),是否可以像模块一样将工作表作为代码导出,因此我可以将工作表完全导入从我的变化看?

I'm aware of some pseudo-solutions to this already, but are either not going to fit into what I need in terms of version control, or will be too much of a lift. 我已经知道一些伪解决方案,但是要么不能满足我在版本控制方面的需要,要么会变得很麻烦。 For one, I can write a VBA script to copy my Excel app to a .xlsx file, which will house any of my sheet changes, and then I can import them into another instance of the app if I wanted to. 首先,我可以编写一个VBA脚本将Excel应用程序复制到一个.xlsx文件中,该文件将容纳我的任何工作表更改,然后,如果需要,我可以将它们导入到该应用程序的另一个实例中。 The problem with that is that I won't be able to include them in my version control. 这样做的问题是,我将无法将它们包括在我的版本控制中。 I also know that I can write scripts in the event Workbook_Open to essentially make my user interface on my sheets programmatically each time the Excel app is opened. 我也知道,我可以在事件Workbook_Open编写脚本,从而基本上在每次打开Excel应用程序时以编程方式在工作表上创建用户界面。 This will solve my problem of committing my code, but is too much of a lift. 这将解决我提交代码的问题,但实在太麻烦了。

Any suggestions or solutions to this problem? 对这个问题有什么建议或解决方案吗?

EDIT: To be clear, I'm aware that I can export my sheets as modules as well. 编辑:明确地说,我知道我也可以将工作表导出为模块。 However, there doesn't seem anything that indicates what the sheet looked like on export. 但是,似乎没有任何迹象表明工作表在导出时的外观。 Consequently, when I import the "sheet", it is imported as a class module. 因此,当我导入“工作表”时,它作为类模块被导入。

I replied to a similar question yesterday Version control system for Excel-VBA code 昨天我回答了类似的问题Excel-VBA代码的版本控制系统

Source controlling sheets is tough. 源代码控制表很困难。 Imagine you serialise data in cells A1,A2,A3 so you might have 假设您序列化单元格A1,A2,A3中的数据,那么您可能

A1:"Foo"
A2:"Bar"
A3:"Baz"
...
A5577: "Kevin"

ie plenty of rows. 即大量的行。 then you insert a row at line at the top, so 3 for example, then all of the rows below would then be out of sync and would throw 5000! 然后您在顶部的行中插入一行,例如3,那么下面的所有行将不同步并抛出5000! mismatchs in a version comparison; 版本比较不匹配; this was quite irritating (when I worked on a system that did this). (当我在执行此操作的系统上工作时),这很烦人。

Perhaps you might like to have a good think about this problem and post a possible solution. 也许您可能想好好考虑一下这个问题并发布可能的解决方案。 Having a quick think about it myself, I wonder if some logic analogous to a linked list would help. 我本人对此进行了快速思考,我想知道一些类似于链表的逻辑是否会有所帮助。 So, one serialises a cell and then also serializes a link to the next cell and one traverse a whole list of cells. 因此,先序列化一个单元格,然后再序列化到下一个单元格的链接,然后遍历整个单元格列表。 Then a single row insert would only require one change, a link insertion. 然后,单行插入将只需要一个更改,即链接插入。

A "sheet" is a Document -type VBComponent , which is a class module, subclassing a specific object type in the host application's object model (here a Worksheet class). “工作表”是Document类型的VBComponent ,它一个类模块,在宿主应用程序的对象模型(此处为Worksheet类) VBComponent类化特定的对象类型。 So the "code" part is exported as a class module; 因此,“代码”部分将导出为类模块; the "document" part is, well, your host document - that content is much more than just cell values and formulas (fonts, cell formats, conditional formats, cell comments, shapes, etc.), and in Office 2007+ those are serialized in an XML format, wrapped-up in a .zip (which is then renamed with a .xlsx extension): for proper source control of the document you need the diffs on the actual XML. “文件”部分,那么,您的主机的文件-即含量不止细胞值和公式(字体,单元格格式,条件格式,单元格批注,形状等),并在Office 2007+那些被序列化以XML格式打包,并以.zip打包(然后以.xlsx扩展名重命名):为了对文档进行适当的源代码控制,您需要与实际XML上的差异。

Moreover, you can't import a document-type VBComponent back into a VBProject , because it's the host application that owns document-type components. 此外,您不能将文档类型的VBComponent导入回VBProject ,因为它是拥有文档类型组件的宿主应用程序。 For example to add a worksheet to ThisWorkbook , you need to use the Excel object model and call ThisWorkbook.Worksheets.Add ; 例如,要将工作表添加到ThisWorkbook ,您需要使用Excel对象模型并调用ThisWorkbook.Worksheets.Add ; ditto for removing them. 同上删除它们。

That's why there is no source control solution for VBA that deals with every single use case: for it to be full-featured, it would have to be able to modify the compressed XML of the document's very structure, while that document is opened , by loading the XML from disk. 这就是为什么没有用于VBA的源代码控制解决方案能够处理每个用例的原因:要使其功能完整,它必须能够在打开该文档的同时修改该文档结构的压缩XML。从磁盘加载XML。

I have this OSS project, Rubberduck , which features integrated Git source control: 我有这个OSS项目Rubberduck ,它具有集成的Git源代码控制功能:

Rubberduck的“源代码控制”面板

It's host-agnostic, so there's nothing host-specific about what it does - but even if some claim "it's simple" , the reality is that, well, it isn't: one person will be happy with being able to restore the code-behind; 它与主机无关,因此它的功能并没有特定于主机的内容,但是即使有人声称“很简单” ,实际情况并非如此:一个人会很高兴能够恢复代码-背后; another will want the cell values and formulas; 另一个将需要单元格的值和公式; another will need to be able to restore shapes and their formats and assigned macros, and another person will complain because they lose their conditional formats when restoring their project from source control: at the end of the day the only way to have a document-type VBComponent under source control, is to extract its XML and keep that under source control... but then, you can't quite restore the document. 另一个人将需要能够恢复形状及其格式和分配的宏,另一个人会抱怨,因为从源代码管理中恢复项目时,他们失去了条件格式:最后,拥有文档类型的唯一方法VBComponent源代码控制下,是提取它的XML,并保持源控制下......不过呢,你不能完全恢复的文件。

The only reasonable solution, is to decouple the VBA code implmentation from the document as much as possible - write VBA code that works regardless of what worksheets are in the workbook the code is hosted in; 唯一合理的解决方案是,尽可能使VBA代码实现与文档脱钩-编写有效的VBA代码,而不管代码所在的工作簿中的工作表是什么; parameterize everything, and have a module that generates the worksheet layouts and formats and whatnot - in other words, don't "design" the worksheets (in Excel), "code" them (in the VBE). 参数化所有内容,并具有一个生成工作表布局和格式等内容的模块-换句话说,不要“设计”工作表(在Excel中),不要“编码”它们(在VBE中)。

As for the data... well, tough luck: source control is meant to be for code , not for data . 至于数据...好运,不幸的是:源代码控制是为了代码 ,而不是数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM