如何组织VBA代码 [英] How to organize VBA code

查看:51
本文介绍了如何组织VBA代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有大量的VBA代码组织得不好,因为它起初是一个小项目,最终变得相当广泛Word加载项随着时间的推移。新的开发人员正在进入并查看代码,并且正在进行一次艰难的
时间导航。我想知道是否有人可以就他们如何找到最有用的VBA代码组织提供一些反馈?
例如,函数应按字母顺序显示,创建日期顺序,按类似功能分组,或者受欢迎程度(使用频率)?

I have a significant amount of VBA code that isn't really organized well because it started off as a small project and ended up becoming a fairly extensive Word Add-In over time. New developers are coming in and looking at the code and are having a hard time navigating it. I wondered if anyone could give some feedback on how they find it most useful to have their VBA code organized? For example, should functions appear in alphabetical order, order of date created, grouped by similar function, or order of popularity (how often they are used)?

感谢您的反馈!

推荐答案

这是一个非常常见的场景,项目从小开始并随着时间的推移而增长。组织和记录代码是值得做的,即使是唯一的开发人员,即你,但是如果要由他人维护则必不可少。

This is a very common scenario, projects start small and grow over time. Organising and documenting code is well worth doing even if a sole developer is involved, ie you, but essential if it is to be maintained by others.

关于如何最好地安排,没有单一的规则代码但通常例程属于不同类别的做事。最明显的事情是将广泛相关的例程分成单独的和直观命名的模块,例如'modEntry'用于启动
和初始化例程,'modFormat' 对于任何涉及更改格式的内容,modMisc甚至。

There's no single rule about how best to arrange code but typically routines fall into different categories of doing stuff. The most obvious thing to do is separate broadly related routines into separate and intuitively named modules, eg 'modEntry' for startup and initialise routines, 'modFormat'  for anything involved in changing formats, modMisc even.

直观地尝试和命名例程,变量和(对于这个项目可能为时已晚)但考虑使用命名约定。它不一定是'匈牙利',但只要它是一致的(我个人使用'n'而不是
而不是'lng'和带有's'而不是'str'的字符串前缀Long变量) 。

Try and name routines intuitively, variables too and (might be too late for this project) but think about using a naming convention. It doesn't have to be 'Hungarian' but as long as it is consistent (personally I prefix Long variables with an 'n' rather than 'lng' and Strings with an 's' rather than 'str').

使用注释,无需说明显而易见但足以解释发生了什么,特别是任何异常或复杂的事情。

Use comments, no need to state the obvious but just enough to explain what's going on and particularly anything unusual or complicated.

如果有多个开发人员将参与修订的作者姓名和日期应记录在评论中。

If multiple developers will be involved revisions with author names and dates should be documented in the comments.

只是一些想法,其他人可能有不同的评论要添加。

Just a few thoughts off the top of my head, others may have different comments to add.

还有一件事,与您的问题没有直接关系,但是所有模块选项明确。

One more thing, not directly related to your question but head all modules Option Explicit.

Peter Thornton

Peter Thornton


这篇关于如何组织VBA代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆