简明Excel VBA(十二) VBA Best Practices

简明Excel VBA

本文集同步于GitHub仓库:# Youchien/concise-excel-vba

0x06 VBA Best Practices

  1. Always have Option Explicit at the top of your code modules to
    enforce variable declaration.
  2. Never write procedures and functions that are longer than a full screen
    as these are hard to understand. Procedures should fit on one screen -
    ie be 40-50 lines long maximum.- ie be 40-50 lines long maximum.
  3. Always prefix your variables so you can quickly identify their datatype.
  4. Never use the Variant datatype unless absolutely necessary.</br>
    :尽量不要使用Variant,要显示的声明具体的数据类型。Variant是VBA中的一种特殊类型,
    所有没有声明的数据类型的变量都默认是Variant型。但Variant型所占的存储空间远大于其他的
    数据类型,所以除非必要,否则应该避免申明变量为Variant型。
  5. Always use the keyword "Call" to call your procedures.
  6. Always put your arguments in parentheses.
  7. Never use Global variables unless absolutely necessary.
    Pass parameters ByVal (ByRef is the default) - only use ByRef where
    you intend to modify the parameter and pass the change back to the caller.
  8. Always use tabs to indent your code to bring structure, never use spaces.
  9. Add "value added" comments which explain why, do not add trivial comments.
  10. Always add an Error Handler to every procedure and function.
  11. Use the line continuation character to make your code more readable and
    to reduce the amount of scrolling.
  12. Never use the Option Base or Option Compare statements.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容