将自动发票生成重置为000/';本年度';在Excel VBA中的新会计年度开始时

将自动发票生成重置为000/';本年度';在Excel VBA中的新会计年度开始时,excel,vba,Excel,Vba,我有一个工作代码,当用户在userform中输入其他详细信息并点击submit时,它会自动以112/2020、113/2020等格式(对于2020财年)增加发票号 我想要的是,根据我的会计年度功能,只要会计年度在4月份发生变化,就将此发票编号重置为001/2021。每年当财政年度增加时,它都应该这样做 下面给出了我用来计算当前会计年度的函数 If Month(dDate) <= 3 Then FinancialYear = Year(dDate) - 1 Else Fi

我有一个工作代码,当用户在userform中输入其他详细信息并点击submit时,它会自动以112/2020、113/2020等格式(对于2020财年)增加发票号

我想要的是,根据我的会计年度功能,只要会计年度在4月份发生变化,就将此发票编号重置为001/2021。每年当财政年度增加时,它都应该这样做

下面给出了我用来计算当前会计年度的函数

If Month(dDate) <= 3 Then
    FinancialYear = Year(dDate) - 1
  Else
    FinancialYear = Year(dDate)
  End If
End Function
在哪里

emptyRow = WorksheetFunction.CountA(nwb.Sheets("Sheet1").Range("A:A")) + 1 'To calculate next empty row

current_year = FinancialYear(Date)
完整的代码是(我没有包括其他不必要的东西):

函数FinancialYear(dDate作为日期)为整数

如果月份(dDate)在生成递增的发票号之前,只需检查当前年度的发票号1
001/2021
是否已存在于您的发票中。通过使用

如果该发票编号存在,则发票计数器已在当前年份中,如果不存在,则需要将计数器重置为0,因为当前年份中尚未写入任何发票

Dim FirstInvoice As String
FirstInvoice = "001" & "/" & current_year

Dim MatchInvoice As Double
On Error Resume Next 'next line will error if invoice 001/2021 does not exist yet
MatchInvoice = Applicarion.WorksheetFunction.Match(FirstInvoice, RangeOfInvoceNumbers, 0)
On Error Goto 0  're-enable error reporting

If MatchInvoice = 0 Then
    'first invoice of the current year does not exist so
    'reset your invoice counter to 0 here
End If

'proceed generating your increment as you did before.
这不会在1中直接重置计数器。1月,但只要在新的一年中生成第一张发票。例如,如果你在6月份写下第一张发票,这甚至是可行的


有点离题:

请注意,使用此格式
001/2021
的排序如下:

001/2020
001/2021
001/2022
002/2020
002/2022
002/2023
<>所以你可以考虑一个更好的格式:

2020/001
2020/002
2021/001
2021/002
2022/001
2022/002
这将自动按年份排序

如果您将实际存储的发票编号保留为数字,如
2020001
,只需使用编号格式
0000”/“000
将其显示为
2020/001
,以提高人类可读性,您甚至可以使用
MAX
功能在发票中查找最新的发票编号,并通过计算
+1
来增加该编号

然后还可以使用
Application.WorksheetFunction.Max
检查是否需要更轻松地重置和递增

Dim FirstInvoiceCurrentYear As Long
FirstInvoiceCurrentYear = cLng(current_year & "001")

Dim CurrentInvoice As Long
CurrentInvoice = Application.WorksheetFunction.Max(RangeOfInvoceNumbers)

Dim NextInvoiceNumber As Long
If FirstInvoiceCurrentYear > CurrentInvoice Then
     NextInvoiceNumber = FirstInvoiceCurrentYear 
Else
     NextInvoiceNumber = CurrentInvoice + 1
End If
所以你可以看到,选择一种更好的格式在某些方面会让你的生活变得更轻松


您只需确保包含发票编号的任何单元格都设置为数字格式
0000”/“000
,然后在该单元格中输入
2020001
,它将自动显示为
2020/001
,但该单元格仍然是您可以使用的数字。

您可以检查前一行发票编号的最后4位数字(例如使用
IIF()
函数):


对于这些简单的问题,我很抱歉,但是语音编号的范围是什么?它是发票编号列吗?我应该将您的代码包括在内还是之外?@RaghavChamadiya是的,只需将
语音编号的范围设置/替换为类似
此工作簿的内容。工作表(“发票”)。范围(“A:A”)
如果您的发票号在工作表
发票
的A列中•如果您像上面那样指定
发票号范围的工作表和范围,则此代码不需要带
语句。非常感谢您为此付出的努力,并且我无法更改发票号的格式,因为它是固定的。@RaghavChamadi该死的,使用起来会容易得多。不过第一个解决方案应该适用于您的格式。
2020/001
2020/002
2021/001
2021/002
2022/001
2022/002
Dim FirstInvoiceCurrentYear As Long
FirstInvoiceCurrentYear = cLng(current_year & "001")

Dim CurrentInvoice As Long
CurrentInvoice = Application.WorksheetFunction.Max(RangeOfInvoceNumbers)

Dim NextInvoiceNumber As Long
If FirstInvoiceCurrentYear > CurrentInvoice Then
     NextInvoiceNumber = FirstInvoiceCurrentYear 
Else
     NextInvoiceNumber = CurrentInvoice + 1
End If
Dim nwb As Workbook
Set nwb = Workbooks.Open("C:\Users\CHAMARA2\Downloads\TestDB.xlsx")

Dim current_year As String
current_year = FinancialYear(Date)

With nwb.Sheets("Sheet1")
    With .Cells(.Rows.Count, 1).End(xlUp).Offset(1)
        .Value = .Row - 1 'serial number
        .Offset(, 1) = Format(Int(Left$(IIf(Right$(.Offset(-1, 1).Value, 4) = current_year, .Offset(-1, 1).Value, 0), 3)) + 1, "000") & "/" & current_year
    End With
End With