当前位置: 首页 > 文档资料 > 简明 Excel VBA >

07 VBA Best Practices

优质
小牛编辑
144浏览
2023-12-01
  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.
    :尽量不要使用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.