1、"&"替换"+".
在很多人编程语言中用“+”来连接串这样容易导致歧义良好习惯是用“&”来连接串.
不正确:
dim sMessage as
sMessage="1"+"2"
正确:
dim sMessage as
sMessage="1" & "2"
注意:"&"后面有个空格.
2.变量命名大小写,语句错落有秩
下面大家比较下以下两段代码:
读懂难度很大代码:
dim SNAME as
dim NTURN as eger
NTURN=0 then
SNAME="sancy" then
end
Do while until NTURN=4
NTRUN=NTURN+1
Loop
End
容易读懂代码:
dim sName as
dim nTurn as eger
nTurn=0 then
sName="sancy" then
end
Do while until nTurn=4
nTurn=nTurn+1
Loop
End
3.在简单选择条件情况下,使用IIf
繁琐代码:
nNum=0 then
sName="sancy"
sName="Xu"
end
简单代码:
sName=IIF(nNum=0,"sancy","Xu")
4.尽量使用Debug.pr进行调试
在很多初学者调试中,用MsgBox来跟踪变量值.其实用Debug.pr不仅可以达到同样功效,而且在最后编译过程中,会被忽略.而MsgBox必须手动注释或删除.
不正确:
MsgBox nName
正确:
Debug.pring nName
5.在重复对某对象属性进行修改时,尽量使用with....end with
6.MsgBox中尽量使用图标
般来说
vbInformation用来提示确认或成功操作消息
vbExclamation用来提示警告消息
vbCritical用来提示危机情况消息
vbQuestion用来提示询问消息
7.在可能情况下使用枚举
枚举格式为
public enum
...
end enum
好处是加快编译速度
最新评论