简明Excel VBA
本文集同步于GitHub仓库:# Youchien/concise-excel-vba
1.8 注释(Comments code)
个人觉得注释起着非常重要的作用 -- ** bluetata ** 11/28/2018 18:40
注释语句是用来说明程序中某些语句的功能和作用;VBA 中有两种方法标识为注释语句。</br>
单引号 '
举例:' 定义全局变量
;可以位于别的语句之尾,也可单独一行。</br>
Rem
举例:Rem 定义全局变量
;只能单独一行
以下列举出了不同级别的注释代码,也可以点击这里查看 VBA Sample Code。
1. 源码概要注释/Source version Comments Code</br>
在每个source文件的最开头
'--------------------------------------
' Creation date : 03/05/2017 (cn)
' Last update : 11/28/2018 (cn)
' Author(s) : Sekito.Lv
' Contributor(s):
' Tested on Excel 2016
'--------------------------------------
2. 区块注释/Use Title Blocks Comments code for Each Macro</br>
在每个Function或者Sub上下,根据个人风格,可以在紧贴在函数上面一行处,
也可以在函数名的下面一行处。
'=======================================================
' Program: DoMemoData
' Desc: Writes memo data to the memo sheet
' Called by: PrintControl
' Call: DoMemoData wbkReport, oStopRow
' Arguments: wbkReport--Name of the report workbook
' oStopRow--Number of the last row to process
' Comments: (1) RunReport initializes the m_oMemoRowNum
' variable
' (2) wksMemo doesn't need to be static. And
' it's over-defined. Fix this at some
' point.
' Changes----------------------------------------------
' Date Programmer Change
' 11/26/2018 Sekito.Lv Written
' 11/28/2018 Sekito.Lv Re-set memo object. This is
' needed at times in Excel 8
' when the report workbook must
' close then re-open.
'=======================================================
Sub DoMemoData(wbkReport As Workbook, oStopRow As Long)
3. 行内注释/Use In-Line Comments
' If this routine was called by the batch routine...
If g_bCalledByBatch Then
'Get the reference of the changing date cell
sDateRef = GetNameVal("ChgDateCell", 0, g_nReference)
' If the date name is empty, return null sDateFormula
If sDateRef = g_sNull Then
sDateFormula = g_sNull
' Else, get the beginning formula in the date cell
Else
sDateFormula = m_wbkReport.Worksheets(1). _
Evaluate(sDateRef).Formula
End If
Else
4. 函数列表注释/List of Function Comments</br>
一般紧挨着源码概要注释下面,与其空一行到两行
'-------------------------------------
' List of functions :
' - 1 - PublicHolidayFr
' - 2 - WorkingDay
' - 3 - WorkableDay
' - 4 - NextWorkingDay
' - 5 - NextWorkableDay
' - 6 - PrevWorkingDay
'-------------------------------------