使用描述性变量命名
代码中的描述性名称和结构有助于使注释变得不必要
Dim ductWidth As Double
Dim ductHeight As Double
Dim ductArea As Double
ductArea = ductWidth * ductHeight
比…更好
Dim a, w, h
a = w * h
当你将数据从一个位置复制到另一个位置时,无论是单元格,范围,工作表还是工作簿,这都非常有用。通过使用以下名称来帮助自己:
Dim myWB As Workbook
Dim srcWS As Worksheet
Dim destWS As Worksheet
Dim srcData As Range
Dim destData As Range
Set myWB = ActiveWorkbook
Set srcWS = myWB.Sheets("Sheet1")
Set destWS = myWB.Sheets("Sheet2")
Set srcData = srcWS.Range("A1:A10")
Set destData = destWS.Range("B11:B20")
destData = srcData
如果在一行中声明多个变量,请确保为每个变量指定一个类型,如:
Dim ductWidth As Double, ductHeight As Double, ductArea As Double
以下只会声明最后一个变量,而第一个变量将保留为 Variant
:
Dim ductWidth, ductHeight, ductArea As Double