可以自动递增每个新的Excel工作表的订单号吗?

huangapple go评论138阅读模式
英文:

Is it possible to auto-increment each new excel sheet with a new order number?

问题

我正在在Excel中建立一个收据表格:可以自动递增每个新的Excel工作表的订单号吗?

正如你所看到的,我有订单号和客户号。我想以对使用该表格最方便的方式自动递增它们。这些将是受保护的单元格,不能更改,因此它们需要自动化。显然,每张新收据都需要一个新的订单号,每个客户都需要一个新的客户号。在Excel中是否可能实现这一点?

英文:

I am building a receipt sheet in excel:可以自动递增每个新的Excel工作表的订单号吗?

As you can see I have order number and customer number. I want to auto-increment these in a way that's most convenient for one that's gonna use the sheet. These will be protected cells that can't be altered, so therefore they need to be automated. Obviously every new receipt would need a new order number and each customer would need a new customer number. Is it even possible to do this in excel?

答案1

得分: 1

我不认为你可以仅使用公式来实现你想要的目标,你需要使用VBA。

你需要将数值存储在一个持久的地方,读取它并最终更新它。

关于更新和保存数值背后的逻辑,现有的信息不足以提供帮助,但有很多保存和加载数值的方法。

你可以使用纯文本文件,也可以使用Workbook.VariablesWorkbook.CustomProperties,或者只是一个隐藏的工作表。

如果这个工作簿只由单个人在一台计算机上使用,而且文件只有一个副本,那么实现这个目标应该相对简单。如果不是,就会变得复杂一些。

如果你正在创建一个文档模板,你需要添加一些内容,以忽略文件创建后的加载/更新逻辑,以便它不会在下次打开工作簿时更改数值。

示例

以下是一个VBScript示例,它将创建一个文件的副本"InvoiceTemplate.xlsx",并将其命名为"Invoice_.xlsx"(其中是订单号),填充订单号单元格,保护工作表,保存并将新文件呈现给用户。

使用方法:
将以下代码块保存为"new_invoice.vbs"。

我假设你的输出文件是一个带有名为"Invoice"的单个工作表的工作簿。在与"new_invoice.vbs"相同的文件夹中将你的模板文件保存为空文件,命名为"InvoiceTemplate.xlsx"。

在同一位置创建一个名为"invoices"的文件夹。

你需要修改模板工作簿,工作表的名称必须是"Invoice",并且需要向包含订单号的单元格添加一个命名范围(名称为"InvoiceNumber")。最后,你需要解锁所有你希望在最终文档中可编辑的单元格。不要保护模板,因为脚本会为输出文档执行此操作。

一旦你创建了脚本文件,修改了模板并创建了所需的文件夹,你可以双击脚本文件,它应该会创建并打开一个新的带有新订单号的发票。

英文:

I don't think you can do what you want purely with formula, your going to need to use VBA.

You need to store the value somewhere that persists, read it and eventually update it.

There's not enough information to help with the logic behind updating and saving the value, but there are many ways to save and load a value.

You can use a plain text file, you can use Workbook.Variables or Workbook.CustomProperties or just a hidden sheet.

If this workbook is used by a single person on one computer and there is only one copy of the file it should be fairly simple to do. If not it becomes a quite a bit more complex.

If your creating a document template you will need to add something to ignore the load/update logic once the file is created so that it doesn't change the value next time you open the workbook.

Example

The following is a VBScript that will create a copy of a file "InvoiceTemplate.xlsx" as "Invoice_*.xlsx" (where * is the order number) populate the Order number cell, protect the sheet, save and then present the new file to the user.

How to use:
Save the following code block as "new_invoice.vbs"

I've assumed your output file is a workbook with a single worksheet named "Invoice". Save an empty copy of your template as "InvoiceTemplate.xlsx" in the same folder as "new_invoice.vbs"

Create a folder called "invoices" in the same location

You will need to modify your template workbook, the worksheet will need a name "Invoice" and you need to add a NamedRange to the cell containing your order number (the name "InvoiceNumber"). Finally you need to unlock all cells that you want to be editable in the final document. Do not protect the template as the script does this for you on the output document.

Once you've created the script file, modified your template and created the required folder, you can double click the script file, and it should create and open a new invoice with a new number.

Sub Main()
    Dim TemplateSheetName
    TemplateSheetName = "Invoice" ' this is the name of the worksheet in your template
    Dim TemplateNamedRange
    TemplateNamedRange = "InvoiceNumber" ' the name of the cell containing the order number in your template

    Dim FSO
    Set FSO = CreateObject("Scripting.FileSystemObject")

    ' path - folder containing this script, the index file and the template file
    Dim Path
    Path = FSO.GetParentFolderName(WScript.ScriptFullName)

    ' invoicepath - destination folder, where generated invoices are saved
    Dim InvoicePath
    InvoicePath = FSO.BuildPath(Path, "invoices")

    ' indexfile - file containing the next available order number
    Dim IndexFile
    Dim IndexFilePath
    IndexFilePath = FSO.BuildPath(Path, "index.txt")

    ' templatefilepath - path to template file
    Dim TemplateFilePath
    TemplateFilePath = FSO.BuildPath(Path, "InvoiceTemplate.xlsx")

    Dim InvoiceIndex
    If Not FSO.FileExists(IndexFilePath) Then
        ' index file does not exist so create it and set to 1
        Set IndexFile = FSO.CreateTextFile(IndexFilePath)
        IndexFile.Write "1"
        IndexFile.Close
        InvoiceIndex = "1"
    Else
        ' index file exists, read the content into InvoiceIndex
        Set IndexFile = FSO.OpenTextFile(IndexFilePath)
        InvoiceIndex = IndexFile.ReadAll
        IndexFile.Close()
    End If

    ' path of file that will be generated
    Dim InvoiceFilePath
    InvoiceFilePath = FSO.BuildPath(InvoicePath, "Invoice_" & InvoiceIndex & ".xlsx")

    ' test to make sure it doesnt exist
    If Not FSO.FileExists(InvoiceFilePath) Then
        FSO.CopyFile TemplateFilePath, InvoiceFilePath
    Else
        ' output file exists already! abort
        MsgBox "IndexFile Error! Invoice #" & InvoiceIndex & " already exists. Update IndexFile"
        Exit Sub
    End If

    ' create an instance of excel
    Dim Excel
    Set Excel = CreateObject("Excel.Application")
    Excel.Visible = False 
    Dim Workbook
    ' open the copy of the template
    Set Workbook = Excel.Workbooks.Open(InvoiceFilePath)
    Dim Worksheet
    ' update the value of the NamedRange "InvoiceNumber" 
    Set Worksheet = Workbook.Worksheets(TemplateSheetName)
    Worksheet.Range(TemplateNamedRange).Value = InvoiceIndex
    ' protect the worksheet to prevent the order number from being changed
    Worksheet.Protect
    ' save changes
    Workbook.Save
    ' show the new file to the user
    Excel.Visible = True

    ' increment the index file
    Set IndexFile = FSO.CreateTextFile(IndexFilePath,True)
    IndexFile.Write InvoiceIndex + 1
    IndexFile.Close
End Sub

Main

huangapple
  • 本文由 发表于 2023年3月12日 18:19:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定