首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Excel模板--在创建文件时,只计算一次函数。

Excel模板--在创建文件时,只计算一次函数。
EN

Stack Overflow用户
提问于 2018-02-22 21:10:10
回答 1查看 38关注 0票数 0

我们的簿记员有一个Excel模板,用于创建新的采购订单。她希望这样做,这样当她从这个模板创建一个新的电子表格时,它将只在购买订单号码单元格中生成一个新的随机数。

为了最小化两个序号相同的可能性,我正在考虑使用时间戳作为种子,而不是实际的随机数,如:=(NOW()-DATE(1970, 1, 1)) * 864 (我知道这不会形成一个适当的Unix时间戳,因为这并不重要,我只是希望它更短一些)。

我的主要问题是,当从该模板创建新文件时,如何将公式放入单元格并确保它只计算一次?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-22 21:46:04

您应该能够修改和使用的示例代码:

代码语言:javascript
运行
复制
Sub tgr()

    Dim wb As Workbook
    Dim wbNew As Workbook       'Only used if copying template sheet to a brand new workbook
    Dim wsTemplate As Worksheet
    Dim wsNew As Worksheet

    Set wb = ActiveWorkbook
    Set wsTemplate = wb.Sheets("Template")  'Change this to the actual name of your template worksheet

    'If copying the template to a new sheet in same workbook
    wsTemplate.Copy After:=wb.Sheets(wb.Sheets.Count)
    Set wsNew = ActiveSheet

    'If copying the template to a brand new workbook, leave commented out if not using
    'Set wbNew = Workbooks.Add
    'wsTemplate.Copy Before:=wbNew.Sheets(1)
    'Set wsNew = wbNew.ActiveSheet
    'Code here to remove blank sheets from the new workbook if desired

    'wsNew.Name = "Actual Sheet Name"    'Update the name of this newly created sheet
    With wsNew.Range("A1")  'Change to the actual cell that needs to only have its calculation occur once
        .Calculate                      'Update the calculation
        .Value = .Value                 'Convert so that the cell only shows its value and is no longer a formula
    End With

End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48937214

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档