組織新的發票號碼

增加發票號並儲存其價值是一項常見任務。使用 CustomDocumentProperties(CDP) 是在同一工作簿中以相對安全的方式儲存此類數字的好方法,但避免僅在未受保護的工作表中顯示相關的單元格值。

附加提示:

或者,你也可以在隱藏的工作表或甚至所謂的非常隱藏的工作表中輸入值(請參閱使用 xlVeryHidden 表 。當然,也可以將資料儲存到外部檔案(例如 ini 檔案,csv 或任何其他型別)或者登錄檔。

示例內容

以下示例顯示

  • 函式 NextInvoiceNo 設定並返回下一個發票號,
  • 一個過程 DeleteInvoiceNo,它完全刪除發票 CDP,以及
  • 一個程式 showAllCDPs 列出了所有名稱的完整 CDP 集合。不使用 VBA,你也可以通過工作簿的資訊列出它們:資訊| 屬性[DropDown:] | 高階屬性| 習慣

你可以通過呼叫上面提到的函式來獲取並設定下一個發票號(最後一個加一個),返回一個字串值以便於新增字首。InvoiceNo 在所有過程中隱式用作 CDP 名稱。

Dim sNumber As String
sNumber = NextInvoiceNo ()

示例程式碼:

Option Explicit

Sub Test()
  Dim sNumber As String
  sNumber = NextInvoiceNo()
  MsgBox "New Invoice No: " & sNumber, vbInformation, "New Invoice Number"
End Sub

Function NextInvoiceNo() As String
' Purpose: a) Set Custom Document Property (CDP) "InvoiceNo" if not yet existing
'          b) Increment CDP value and return new value as string
' Declarations
  Dim prop As Object
  Dim ret  As String
  Dim wb   As Workbook
' Set workbook and CDPs
  Set wb = ThisWorkbook
  Set prop = wb.CustomDocumentProperties

  ' -------------------------------------------------------
  ' Generate new CDP "InvoiceNo" if not yet existing
  ' -------------------------------------------------------
    If Not CDPExists("InvoiceNo") Then
    '  set temporary starting value "0"
       prop.Add "InvoiceNo", False, msoPropertyTypeString, "0"
    End If

  ' --------------------------------------------------------
  ' Increment invoice no and return function value as string
  ' --------------------------------------------------------
       ret = Format(Val(prop("InvoiceNo")) + 1, "0")
  ' a) Set CDP "InvoiceNo" = ret
       prop("InvoiceNo").value = ret
  ' b) Return function value 
       NextInvoiceNo = ret
End Function

Private Function CDPExists(sCDPName As String) As Boolean
' Purpose: return True if custom document property (CDP) exists
' Method: loop thru CustomDocumentProperties collection and check if name parameter exists
' Site: cf. http://stackoverflow.com/questions/23917977/alternatives-to-public-variables-in-vba/23918236#23918236
' vgl.: https://answers.microsoft.com/en-us/msoffice/forum/msoffice_word-mso_other/using-customdocumentproperties-with-vba/91ef15eb-b089-4c9b-a8a7-1685d073fb9f
' Declarations
  Dim cdp As Variant      ' element of CustomDocumentProperties Collection
  Dim boo As Boolean      ' boolean value showing element exists
  For Each cdp In ThisWorkbook.CustomDocumentProperties
    If LCase(cdp.Name) = LCase(sCDPName) Then
       boo = True      ' heureka
       Exit For        ' exit loop
    End If
  Next
  CDPExists = boo          ' return value to function
End Function

Sub DeleteInvoiceNo()
' Declarations
  Dim wb     As Workbook
  Dim prop   As Object
' Set workbook and CDPs
  Set wb = ThisWorkbook
  Set prop = wb.CustomDocumentProperties

' ----------------------
' Delete CDP "InvoiceNo"
' ----------------------
 If CDPExists("InvoiceNo") Then
    prop("InvoiceNo").Delete
 End If

結束子

Sub showAllCDPs()
' Purpose: Show all CustomDocumentProperties (CDP) and values (if set)
' Declarations
  Dim wb      As Workbook
  Dim cdp     As Object

  Dim i       As Integer
  Dim maxi   As Integer
  Dim s       As String
' Set workbook and CDPs
  Set wb = ThisWorkbook
  Set cdp = wb.CustomDocumentProperties
' Loop thru CDP getting name and value
  maxi = cdp.Count
  For i = 1 To maxi
    On Error Resume Next    ' necessary in case of unset value
    s = s & Chr(i + 96) & ") " & _
            cdp(i).Name & "=" & cdp(i).value & vbCr
  Next i
' Show result string
  Debug.Print s
End Sub