0

I have a button in a template (XLT) that saves the new workbook to a directory. The filename needs to auto-increment from the one used previously. I can't rely on a directory search as the documents may be moved/deleted over time.

Is it possible to auto-increment a cell in the template file, from code running in the new workbook?

4

1 回答 1

0

只要可以保存模板,您就可以将值存储在名称中(这些最常用于存储范围,但可用于存储常量)。

创建名称时,只需在 RefersTo 区域中键入一个值而不是范围。然后你可以像这样访问它:

Sub WhatsTheValue()

    Dim intValue As Integer

    intValue = Evaluate(ThisWorkbook.Names("NextValue").RefersTo)
    ThisWorkbook.Names("NextValue").Value = intValue + 1

    MsgBox intValue & vbNewLine & Evaluate(ThisWorkbook.Names("NextValue").RefersTo)

End Sub

代码将获取 NextValue Name 的当前值,将其分配给 intValue 变量并立即递增 Name。

当然,您每次都必须保存模板。如果多人访问该模板,这可能会导致问题。

于 2013-07-30T13:09:39.830 回答