簡(jiǎn)明Excel VBA
本文集同步于GitHub倉(cāng)庫(kù):# Youchien/concise-excel-vba
1.8 注釋(Comments code)
個(gè)人覺得注釋起著非常重要的作用 -- ** bluetata ** 11/28/2018 18:40
注釋語句是用來說明程序中某些語句的功能和作用纱烘;VBA 中有兩種方法標(biāo)識(shí)為注釋語句稳诚。</br>
單引號(hào) '
舉例:' 定義全局變量
哗脖;可以位于別的語句之尾,也可單獨(dú)一行扳还。</br>
Rem
舉例:Rem 定義全局變量
才避;只能單獨(dú)一行
以下列舉出了不同級(jí)別的注釋代碼,也可以點(diǎn)擊這里查看 VBA Sample Code氨距。
1. 源碼概要注釋/Source version Comments Code</br>
在每個(gè)source文件的最開頭
'--------------------------------------
' Creation date : 03/05/2017 (cn)
' Last update : 11/28/2018 (cn)
' Author(s) : Sekito.Lv
' Contributor(s):
' Tested on Excel 2016
'--------------------------------------
2. 區(qū)塊注釋/Use Title Blocks Comments code for Each Macro</br>
在每個(gè)Function或者Sub上下桑逝,根據(jù)個(gè)人風(fēng)格,可以在緊貼在函數(shù)上面一行處俏让,
也可以在函數(shù)名的下面一行處楞遏。
'=======================================================
' 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. 行內(nèi)注釋/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. 函數(shù)列表注釋/List of Function Comments</br>
一般緊挨著源碼概要注釋下面,與其空一行到兩行
'-------------------------------------
' List of functions :
' - 1 - PublicHolidayFr
' - 2 - WorkingDay
' - 3 - WorkableDay
' - 4 - NextWorkingDay
' - 5 - NextWorkableDay
' - 6 - PrevWorkingDay
'-------------------------------------