1、"&"替換"+".
在很多人的編程語言中,用“+”來連接字符串,這樣容易導(dǎo)致歧義。良好的習(xí)慣是用“&”來連接字符串.
不正確:
dim sMessage as string
sMessage="1"+"2"
正確:
dim sMessage as string
sMessage="1" & "2"
注意:"&"的后面有個(gè)空格.
2.變量命名大小寫,語句錯(cuò)落有秩
下面大家比較一下以下兩段代碼:
讀懂難度很大的代碼:
dim SNAME as string
dim NTURN as integer
if NTURN=0 then
if SNAME="sancy" then
end if
Do while until NTURN=4
NTRUN=NTURN+1
Loop
End if
容易讀懂的代碼:
dim sName as string
dim nTurn as integer
if nTurn=0 then
if sName="sancy" then
end if
Do while until nTurn=4
nTurn=nTurn+1
Loop
End if
3.在簡(jiǎn)單的選擇條件情況下,使用IIf()函數(shù)
繁瑣的代碼:
if nNum=0 then
sName="sancy"
else
sName="Xu"
end if
簡(jiǎn)單的代碼:
sName=IIF(nNum=0,"sancy","Xu")
4.盡量使用Debug.print進(jìn)行調(diào)試
在很多初學(xué)者的調(diào)試中,用MsgBox來跟蹤變量值.其實(shí)用Debug.print不僅可以達(dá)到同樣的功效,而且在程序最后編譯過程中,會(huì)被忽略.而MsgBox必須手動(dòng)注釋或刪除.
不正確:
MsgBox nName
正確:
Debug.pring nName
5.在重復(fù)對(duì)某一對(duì)象的屬性進(jìn)行修改時(shí),盡量使用with....end with
6.MsgBox中盡量使用圖標(biāo)
一般來說
vbInformation用來提示確認(rèn)或成功操作的消息
vbExclamation用來提示警告的消息
vbCritical用來提示危機(jī)情況的消息
vbQuestion用來提示詢問的消息
7.在可能的情況下使用枚舉
枚舉的格式為
public enum
...
end enum
好處是加快編譯速度