Even simple macros and custom functions can be difficult to read. You can make them easier
to understand by entering explanatory text in the form of comments. You add comments by
preceding the explanatory text with an apostrophe.
For example, the following shows the Discount function with comments:
Function Discount(quantity, price)
'Calculate the discount for orders >= 100 units
'No discount for fewer than 100 units
'Arguments:
' quantity is the quantity of the item
' price is the price of the item
'Function returns the discount amount
'Created by K. Ming Leung, Oct. 28, 2006
If quantity >= 100 Then 'if more than 100 units
Discount = quantity * price * 0.1 'calculate a 10% discount
Else 'otherwise
Discount = 0 'no discount
End If
Discount = Application.Round(Discount, 2) 'round to 2 decimal places
End Function
Adding comments like these makes it easier for you or others
to maintain your VBA code as time passes. If you need to make a change to the code in the
future, you’ll have an easier time understanding what you did originally.