簡明Excel VBA(五)注釋(Comments code)

簡明Excel VBA

本文集同步于GitHub倉庫:# Youchien/concise-excel-vba

1.8 注釋(Comments code)

個(gè)人覺得注釋起著非常重要的作用 -- ** bluetata ** 11/28/2018 18:40

注釋語句是用來說明程序中某些語句的功能和作用;VBA 中有兩種方法標(biāo)識為注釋語句。</br>
單引號 ' 舉例:' 定義全局變量;可以位于別的語句之尾,也可單獨(dú)一行。</br>
Rem 舉例:Rem 定義全局變量;只能單獨(dú)一行

以下列舉出了不同級別的注釋代碼,也可以點(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
'-------------------------------------
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。