excel-vba 使用描述性变量命名
示例
代码中的描述性名称和结构有助于使注释变得不必要
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