excel-vba VBA Best Practices Use descriptive variable naming


Example

Descriptive names and structure in your code help make comments unnecessary

Dim ductWidth  As Double
Dim ductHeight As Double
Dim ductArea   As Double

ductArea = ductWidth * ductHeight

is better than

Dim a, w, h

a = w * h

This is especially helpful when you are copying data from one place to another, whether it's a cell, range, worksheet, or workbook. Help yourself by using names such as these:

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

If you declare multiple variables in one line make sure to specify a type for every variable like:

Dim ductWidth As Double, ductHeight As Double, ductArea As Double

The following will only declare the last variable and the first ones will remain Variant:

Dim ductWidth, ductHeight, ductArea As Double