如何使用excel vb脚本来按时间自动生成单据编号
时间: 2023-05-15 10:07:50 浏览: 202
您可以使用以下代码来按时间自动生成单据编号:
Sub 自动生成单据编号()
Dim prefix As String
Dim suffix As String
Dim num As Integer
Dim dateStr As String
Dim code As String
prefix = "JD" '单据前缀
suffix = "" '单据后缀
num = 1 '初始编号
dateStr = Format(Date, "yyyymmdd") '当前日期
code = prefix & dateStr & "-" & Format(num, "000") & suffix '生成单据编号
'查找最后一行
Dim lastRow As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
'查找最后一个单据编号
Dim lastCode As String
lastCode = Cells(lastRow, "A").Value
'如果最后一个单据编号的日期与当前日期相同,则编号加1
If Left(lastCode, 9) = prefix & dateStr Then
num = Val(Right(lastCode, 3)) + 1
code = prefix & dateStr & "-" & Format(num, "000") & suffix
End If
'将单据编号写入当前行
Cells(lastRow + 1, "A").Value = code
End Sub
您可以将此代码复制到Excel的VB编辑器中,并将其绑定到一个按钮或快捷键上,以便在需要时快速生成单据编号。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)